g ++链接问题:在函数`

系统教程 行业动态 更新时间:2024-06-14 16:52:52
g ++链接问题:在函数`_start'中:(。text + 0x20):对'main'的未定义引用(g++ link problems: In function `_start': (.text+0x20): undefined reference to `main')

我得到一个未定义的主要错误引用 - 即使我已经定义了main,和(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(); }

这是控制台输出:

$ 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

为什么链接器找不到main()函数?

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:

// ################################################ //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?

最满意答案

据我所知,你没有编译文件proj1_unittest.cpp(因为你的代码注释有它)/ proj1_unittest.c(正如你的控制台输出暗示)。

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).

更多推荐

本文发布于:2023-04-05 12:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/877b211e4b40c011354be4b498f7e1d7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   链接

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!