C++ g++ 链接问题:在函数‘_start’中:(.text+0x20):对‘main’的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18267625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++ link problems: In function `_start': (.text+0x20): undefined reference to `main'
提问by Homunculus Reticulli
I am getting an undefined reference to main error - even though I have defined main, and (AFAICT), I have linked it correctly. Here is my code and the commands I used:
我收到了对主要错误的未定义引用 - 尽管我已经定义了主要和(AFAICT),但我已正确链接它。这是我的代码和我使用的命令:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOne(0));
EXPECT_EQ(2,addOne(1));
EXPECT_EQ(40320, addOne(40319));
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1));
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here is the console output:
这是控制台输出:
$ g++ -isystem -pthread -c ${SOURCE_DIR}/proj1.c -o ${SOURCE_DIR}/proj1.o
$ g++ -o ${SOURCE_DIR}/mytest ${SOURCE_DIR}/*.o -L${GTEST_DIR} libgtest.a
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
Why is the main()
function not been found by the linker?
为什么main()
链接器找不到该函数?
回答by Ian Miller
As far as I can see you are not compiling file proj1_unittest.cpp (as your code comment has it) / proj1_unittest.c (as your console output implies).
就我所见,您没有编译文件 proj1_unittest.cpp (如您的代码注释所示)/ proj1_unittest.c (如您的控制台输出所暗示的那样)。