crt1.o:在函数`_start'中: - Linux中对'main'的未定义引用

问题描述 投票:30回答:4

我正在将应用程序从Solaris移植到Linux

链接的目标文件没有定义main()。但是在Solaris中正确地完成了编译和链接,并生成了可执行文件。在Linux中我得到这个错误

    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main' 

我的问题是,我不能包含新的.c / .o文件,因为它是一个巨大的应用程序,并已运行多年。我怎样才能摆脱这个错误?

makefile的代码提取:

RPCAPPN = api
LINK = cc 

    $(RPCAPPN)_server: $(RPCAPIOBJ)
            $(LINK) -g $(RPCAPIOBJ) -o $(RPCAPPN)_server $(IDALIBS) $(LIBS) $(ORALIBS) $(COMMONLIB) $(LIBAPI) $(CCLIB) $(THREADLIB) $(DBSERVERLIB) $(ENCLIB)
linux main undefined-reference
4个回答
32
投票

尝试将-nostartfiles添加到您的链接器选项,即

$(LINK) -nostartfiles -g ...

来自gcc documentation

-nostartfiles
    Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used. 

这会导致crt1.o不被链接(默认情况下它通常是链接的) - 通常仅在您实现自己的_start代码时使用。


22
投票

编译-shared时必须使用.so链接选项


2
投票

当我尝试使用boost构建一个新的测试项目时,我得到了类似的结果,结果发现我错过了一个声明:

#define BOOST_TEST_MODULE <yourtestName>

0
投票

编译一个连接了C ++组件的Fortran程序时,我得到了类似的结果。在我的例子中,CMake未能检测到Fortran应该用于最终链接。 make返回的消息随后结束

[100%] Linking CXX executable myprogram
/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
make[3]: *** [myprogram] Error 1
make[2]: *** [CMakeFiles/myprogram.dir/all] Error 2
make[1]: *** [CMakeFiles/myprogram.dir/rule] Error 2
make: *** [myprogram] Error 2

解决方案是添加

set_target_properties(myprogram PROPERTIES LINKER_LANGUAGE Fortran) 

到CMakeLists.txt,以便make打印出来:

[100%] Linking Fortran executable myprogram
[100%] Built target myprogram
© www.soinside.com 2019 - 2024. All rights reserved.