c - Linking error: Undefined reference to functions that're defined in a separate file? -
i'm forgetting obvious that'll solve this. while there's other questions on same issue, none of solutions have been applicable situation.
i have main file, sim.c, header file net.h, header file friends.h, , file functions, net.c. have makefile, created gmakemake > makefile, , contents 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 functions in net.c , defines them stubs. copied , pasted them create function headers, there shouldn't typos.
my net.c file contains:
#include "net.h"
yet time function in sim.c tries phone call function in net.c, errors on line with:
"undefined reference `function_name`".
how can create sim.c able access functions in net.c?
the message undefined reference 'function_name'
implies of object files you're giving linker, none of them has definition function_name
. means either
function_name
-- 'as compiled' mean of various preprocessor options utilize on it. since show neither link command line nor contents of net.c
, can't tell problem.
edit
with edit, can see have first problem -- when seek link sim
, not include net.o
on link command line. commonly, link sim
makefile entry like:
sim: sim.o net.o $(cc) $(cflags) $(ldflags) -o $@ $^ $(ldlibs)
or more just:
sim: sim.o net.o
relying on default create actions linking object files
c make
No comments:
Post a Comment