HB-
HB-

Reputation: 627

Linking error: Undefined reference to functions that're defined in a separate file?

I'm probably forgetting something obvious that'll solve this. While there's other questions on SO with the same issue, none of the solutions have been applicable to my situation.

I have a main file, sim.c, a header file net.h, a header file friends.h, and a file with the functions, net.c. I have a makefile, which I created with gmakemake > Makefile, and its contents are very basic.

Header.mak (makefile template):

CFLAGS = -Wall -Wextra -pedantic -std=c99 -ggdb
LFLAGS = -ggdb
LDFLAGS =

Makefile relevant contents:

CPP_FILES =
C_FILES =       net.c sim.c
PS_FILES =
S_FILES =
H_FILES =       net.h friends.h
SOURCEFILES =   $(H_FILES) $(CPP_FILES) $(C_FILES) $(S_FILES)
.PRECIOUS:      $(SOURCEFILES)
OBJFILES =

#
# Main targets
#

all:    net sim

net:       net.o $(OBJFILES)
        $(CC) $(CFLAGS) -o net net.o $(OBJFILES) $(CLIBFLAGS)

sim:       sim.o $(OBJFILES)
        $(CC) $(CFLAGS) -o sim sim.o $(OBJFILES) $(CLIBFLAGS)

#
# Dependencies
#

net.o:     net.h
sim.o:     net.h

My sim.c file contains:

#include "net.h"
#include "friends.h"

My header file contains the functions in net.c and defines them all as stubs. I copied and pasted them to create the function headers, so there shouldn't be any typos.

My net.c file contains:

#include "net.h"

Yet any time a function in sim.c tries to call a function in net.c, it errors on that line with:

"undefined reference to `function_name`".

How can I make sim.c able to access the functions in net.c?

Upvotes: 4

Views: 25317

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126536

The message undefined reference to 'function_name' implies that of all the object files you're giving to the linker, none of them has a definition for function_name. That means that either

  • You're not linking with net.o
  • net.c (as compiled) does not contain a definition for function_name -- by 'as compiled' I mean with all of the various preprocessor options you use on it.

Since you show neither your link command line nor the contents of net.c, we can't tell which is the problem.

edit

with your edit, we can see clearly that you have the first problem -- when you try to link sim, you do not include net.o on the link command line. Most commonly, you would link sim with a makefile entry like:

sim: sim.o net.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

or more simply just:

sim: sim.o net.o

relying on the default make actions for linking object files

Upvotes: 3

rubmz
rubmz

Reputation: 2029

For the two functions "see" each other make sure that:

  1. Both c/cpp files are included in the makefile
  2. Both definition(c/cpp) and declaration (h) files contains the same definition of the function: name/params/return value
  3. The function being called must not be static.
  4. Make sure you don't declare (or include) the same type with different structure in the source files.

That should do, unless you are using a very old complier with even more evil things that can go wrong ;)

Upvotes: 2

Related Questions