makefile错误:对main的未定义引用 [英] makefile error: undefined reference to main
问题描述
我正在尝试使用make来编译我的代码.通常我会这样编译我的代码:
I am trying to compile my piece of code using make. Normally I compile my code like this:
mipsisa32r2el-timesys-linux-gnu-g++ -o testing -I/usr/include/libxml2 -L/develop/xc4/rootfs/parsecpp/lib -L/develop/xc4/rootfs/parsecpp/sqlite-mips2/lib -I/develop/xc4/rootfs/parsecpp/sqlite-mips2/include db.cpp main.cpp networkinterfacemodule.cpp network.cpp multiplex.cpp program.cpp service.cpp -lsqlite3 -lxml2
为了摆脱这个长命令,我尝试编写一个makefile:
To get rid of this long command I tried to write a makefile:
CC= mipsisa32r2el-timesys-linux-gnu-g++
export LD_LIBRARY_PATH=:/parsecpp/sqlite-mips2/lib:/parsecpp/lib:/tmp/vixs_temp/DirectFB/single_core/lib
CFLAGS=-I/usr/include/libxml2 -I/develop/xc4/rootfs/parsecpp/sqlite-mips2/include
LDFLAGS=-L/develop/xc4/rootfs/parsecpp/lib -L/develop/xc4/rootfs/parsecpp/sqlite-mips2/lib
LIBS = -lsqlite3 -lxml2
PROG=testing
all: main.o db.o mod.o multiplex.o network.o networkinterfacemodule.o program.o service.o
$(CC) -o $(PROG) $(CFLAGS) $(LDFLAGS) main.o db.o mod.o multiplex.o network.o networkinterfacemodule.o program.o service.o $(LIBS)
main.o: main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) main.cpp db.cpp networkinterfacemodule.cpp mod.cpp multiplex.cpp network.cpp program.cpp service.cpp $(LIBS)
db.o: db.cpp
$(CC) $(CFLAGS) $(LDFLAGS) db.cpp $(LIBS)
mod.o: mod.cpp
$(CC) $(CFLAGS) $(LDFLAGS) mod.cpp $(LIBS)
multiplex.o: multiplex.cpp
$(CC) $(CFLAGS) $(LDFLAGS) multiplex.cpp $(LIBS)
network.o: network.cpp
$(CC) $(CFLAGS) $(LDFLAGS) network.cpp $(LIBS)
networkmoduleinterface.o: networkinterfacemodule.cpp
$(CC) $(CFLAGS) $(LDFLAGS) networkinterfacemodule.cpp $(LIBS)
program.o: program.cpp
$(CC) $(CFLAGS) $(LDFLAGS) program.cpp $(LIBS)
service.o: service.cpp
$(CC) $(CFLAGS) $(LDFLAGS) service.cpp $(LIBS)
clean:
rm -rf *o testing
然后我收到此错误:
/opt/timesys/linux-gnu/toolchain/bin/../../toolchain/lib/crt1.o: In function `__start':
(.text+0xc): undefined reference to `main'
/opt/timesys/linux-gnu/toolchain/bin/../../toolchain/lib/crt1.o: In function `__start':
(.text+0x10): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [db.o] Error 1
有人可以帮助我吗?
推荐答案
每当您仅编译文件而不链接文件时,请使用"-c"标志.
Whenever you are just compiling a file and not linking it, use the "-c" flag.
例如:-
db.o: db.cpp
$(CC) -c $(CFLAGS) $(LDFLAGS) db.cpp $(LIBS)
此外,在编译时,不需要向编译器提供"$(LIBS)",仅在链接时提供它们.您也不需要链接器标志,因为使用"-c"标志时不会调用链接器.
Also, while compiling, there's no need to provide "$(LIBS)" to the compiler, only provide them when linking. Nor do you need the linker flags since linker is not called when using the "-c" flag.
所以你可以写
db.o: db.cpp
$(CC) -c $(CFLAGS) db.cpp
已更新(基于评论):-
UPDATED (based on comments):-
链接文件时,链接器期望只有一个main
功能.在上述情况下,主函数未在db.cpp
中定义,因此尽管编译成功,但链接器由于找不到main
函数而引发了错误.
When linking the files, the linker expects one and only one main
function. In the above case, the main function is not defined in db.cpp
and hence although compilation succeeds, the linker throws an error as it cannot find the main
function.
这篇关于makefile错误:对main的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!