【问题标题】:Compilation problem in the standard x86_64 libraries标准 x86_64 库中的编译问题
【发布时间】:2010-05-25 20:08:08
【问题描述】:

我在编译我编写的程序时遇到问题。我有两个包含相同包含的不同文件,但只有一个在使用 g++ 编译时会生成以下错误

/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status

我在标题中包含的文件如下:

#include <google/sparse_hash_map>
using google::sparse_hash_map;

#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>

using std::priority_queue;
using std::stack;
using std::vector;

using __gnu_cxx::hash_map;
using __gnu_cxx::hash; 

using namespace std;

在互联网上搜索这两条线并没有对我有任何帮助。我将非常感谢任何建议。谢谢

【问题讨论】:

  • 您能否给我们完整的源代码,或者至少在文件中 main() 之前的几行编译失败?
  • 大家好,感谢您的提示。我听从了你的一些建议,并意识到我不明白我编程方式对 main 的需求。以为我可以编译为一个函数。看来我还有很多东西要学。

标签: c++ linker compiler-errors


【解决方案1】:

要构建两个独立的程序,您需要两个源文件来定义main() 函数。

要从两个源文件构建单个程序 - 首先使用-c 选项编译每个文件(仅编译) - 您将获得两个.o 文件,然后链接 这些文件放在一起。像这样的:

$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o

从两个源文件构建二进制prog

如果你需要链接某个库,你必须用-I 将编译器指向它的头文件和带有-L 标志的对象,然后告诉链接器用-l 实际引用该库。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您需要一个main 函数,但您没有。如果您确实main 函数,请显示更多代码。

    【讨论】:

      【解决方案3】:

      看起来main 没有定义。您是否为您的第二个程序定义了一个?您能否发布有关无法链接的源正文的更多详细信息?

      【讨论】:

      • 谢谢。这就是我需要弄清楚为什么我的代码不起作用的全部内容。需要定义一个 main 来调用我的函数。
      猜你喜欢
      • 1970-01-01
      • 2013-09-16
      • 2014-12-18
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      相关资源
      最近更新 更多