Compiling: undefined reference

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

3 posts / 0 new
Last post
Compiling: undefined reference

Submitted by fsimon on Wed, 07/08/2009 - 15:46. General Questions
Hi

I have two files that make reference to a function. The function is declared as extern in one file and declare and define in the other file. Compiling generates the error: undefined reference to the function. If I move the file.o (object file) in the makefile file, the error goes away. I am having this problem compiling several of the source codes in the makefile. These source codes compiled on IRIX6.2 without problem. I am moving these codes to SUSE Linux using openMotif 2.2.4+.

My question is:
Does it matter where the object files are in the makefile?
i.e. OBJS = a.o \
b.o \
c.o \
d.o

moving c.o before a.o solves the problem for these files. But similar problems still exist

Thank so much

Yuriy Syrota

Sounds like something impossible. Could you post simple example clearly demonstrating your issue?

fredk

You should not declare a function as extern in the source file.

What you should do is declare it as extern in an include file, then include that header file in both of the source code files. In one of the source files, you define the function (not static, and no extern key word).

For example:

MyHeader.h:
--------------
extern int MyFunction( int arg1, int arg2);

Source1.c:
-----------
#include "MyHeader.h"

void MyFunction( int arg1, int arg2 )
{
/* body */
}

Source2.c:
-------------
#include "MyHeader.h"

int SomeFunction()
{
/* declarations and code */

MyFunction( i, j );
}