简体   繁体   English

g ++链接问题:在函数`_start'中:(。text + 0x20):对'main'的未定义引用

[英]g++ link problems: In function `_start': (.text+0x20): undefined reference to `main'

I am getting an undefined reference to main error - even though I have defined main, and (AFAICT), I have linked it correctly. 我得到一个未定义的主要错误引用 - 即使我已经定义了main,和(AFAICT),我已正确链接它。 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? 为什么链接器找不到main()函数?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 函数`_start':(。text + 0x20):未定义引用`main'colle2:ld返回1退出状态 - In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status crt1.o:在函数_start中:(.text + 0x20):对main的未定义引用 - crt1.o: In function `_start': (.text+0x20): undefined reference to `main' (.text + 0x20):尝试编译.cpp文件时对“ main”的未定义引用 - (.text+0x20): undefined reference to `main' When trying to Compile .cpp file g ++链接错误:“对'main'的未定义引用 - g++ link error: 'undefined reference to 'main' MinGW / G ++ / g95链接错误-libf95未定义对“ MAIN_”的引用 - MinGW/G++/g95 link error - libf95 undefined reference to `MAIN_' 与 g++ 链接时对 function 的未定义引用 - Undefined reference to a function when linking with g++ g ++对独立函数的未定义引用 - g++ undefined reference to a free standing function G ++ - 未定义对已定义的成员函数的引用 - G++ - Undefined Reference to member function that is defined 将单独的头文件和类定义文件链接到主函数文件 - G++ 返回对重载构造函数的“未定义引用” - Linking separate header and class definition files to a main function file - G++ returns "undefined reference to" overloaded constructors 链接器错误,使用g ++链接到gcc编译的库,对该函数的未定义引用 - Linker error, using g++ to link to a library compiled by gcc, undefined reference to that function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM