4

就处理编译/链接错误而言,我有点新手。

我正在使用大型 C++ 代码(也有一些 C 文件)。我已经在使用 g++ 编译的 Mac 上成功运行它。现在我需要在基于 Linux 的集群上运行它,因为它在我的 Mac 上太慢了。该代码由我必须编译的几个库以及我自己的使用这些库的代码组成。

我可以使用默认的 g++ 编译器编译集群上的所有代码。然而,不幸的是,我发现我需要使用 gcc/4.7.2 进行编译,这样代码才能与集群上的其他软件一起使用。我一直在努力解决编译和链接错误。到目前为止,它们都与 C++ 与 C 的问题有关。例如,我不得不在一些 C 文件中添加“extern”。我不得不将包含从 C++ 更改为 C 头文件。

我的问题似乎与一个特定的库有关(我已经成功处理了所有其他库)。它在其目录中编译。但是当我进入我的运行目录时,我得到了各种似乎与构成这个问题库的代码有关的错误。我的猜测是它们主要与找不到标准库有关。我只是不明白我的包含有什么问题,如果有人能看看我在这个库的 makefile 中有什么,我会非常感激。这个库正式基于“C++”,但它的文件中似乎确实有很多 C 风格的代码。

有太多的错误(页面)和太多的代码来发布所有内容。我希望我发布的内容已经足够,如果有帮助可以添加。我无法在我的代码中找到它们的确切来源,因为它们非常神秘;例如:

Code.cpp:(.text+0x35): undefined reference to `std::cout'

我的 makefile 包含和编译器选项如下。我最初使用 QT 在我的 Mac 上创建了这个。我对其进行了修改,以便与 gcc/4.7.2 上的 Linux 集群一起使用。所以完全有可能有点乱。

首先我做(终端中的命令行):

模块加载 gcc/4.7.2

然后makefile是:

CC = gcc

定义 = -DIPMGEMPLUGIN -DNOPARTICLEARY -D__unix

CFLAGS = -c -g -O2 -pedantic -fno-nonansi-builtins -D__unix -m64

CXXFLAGS = -pipe -O2 -Wall -W -fPIC $(DEFINES) -lstdc++ -m64

INCPATH = -I。\

             -I/mounts/apps/gcc/4.7.2/ \

             -I/mounts/apps/gcc/4.7.2/bin/ \

             -I/mounts/apps/gcc/4.7.2/bin/x86_64-unknown-linux-gnu/4.7.2 \

             -I/mounts/apps/gcc/4.7.2/lib64 \

             -I/mounts/apps/gcc/4.7.2/bin/include \

             -I/mounts/apps/gcc/4.7.2/bin/include/c++ \

             -I/mounts/apps/gcc/4.7.2/4.7.2/bin/include/c++/4.7.2 \

AR = ar cq RANLIB = ranlib -s 目标 = mylib.a

.后缀:.o .c .cpp .cc .cxx .C

.cpp.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.Co: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.co: $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

现在我得到的错误种类:

Code.o: 在函数 _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.part.8': Code.cpp:(.text+0x12): undefined reference tostd::basic_ios >::clear(std::_Ios_Iostate)' Code.o: 在函数 _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.constprop.101': Code.cpp:(.text+0x35): undefined reference tostd::cout' Code.cpp:(.text+0x3a): 未定义引用`std:: basic_ostream >& std::__ostream_insert

(std::basic_ostream >&, char const*, long)' Code.cpp:(.text+0x3f): undefined reference to std::cout' Code.cpp:(.text+0x49): undefined reference tostd::cout' Code.cpp:(.text+0x53): undefined reference to std::cout' Code.o: In function _ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode。 constprop.99': Code.cpp:(.text+0x93): undefined reference to std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)' Code.o: In function _ZNSt18basic_stringstreamIcSt11char_traitsIcESaIcEEC1ESt13_Ios_Openmode.constprop.96': Code.cpp:(.text+0x103): undefined reference to std::ios_base::ios_base()' Code.cpp:(.text+0x10b): undefined reference tovtable for std::basic_ios' Code.cpp :(.text+0x11b): 对VTT for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x15d): undefined reference to std::basic_iostream >::basic_iostream()' 的未定义引用 Code.cpp:(.text+0x16c): vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x174): undefined reference to std::basic_stringstream, std::allocator >' 对 vtable 的未定义引用.cpp:(.text+0x17c):vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x184): undefined reference to 对 std::的 vtable 的未定义引用 basic_streambuf >'

更进一步:

gl3.cpp:(.text+0x18d): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1a1): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1b5): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1c9): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1dd): undefined reference to `operator new[](unsigned long)'

/data/place/number/account/CodeDirectory/../ProblemLibraryDirectory/libProblem.a(gl3.o): 在函数Other::free_internal()': gl3.cpp:(.text+0x251): undefined reference to操作符中删除'gl3.cpp:(.text+0x262): ...

这对任何人都意味着什么吗?

4

1 回答 1

1

您正在使用 C++ 函数,例如newstd::*函数,因此您必须使用 C++ 编译器进行编译(除非您将 C 编译版本与 C++ 库链接)。GCC 是 C 编译器,G++ 是 GNU C++ 编译器,所以你应该使用 G++。

我在您的一条评论中看到由于某种原因您必须使用 GCC。如果是这种情况,那么您将不得不从您的程序中删除所有 C++ 特定代码(例如std::*new/ delete),或者与 C++ 库链接。但是,我认为您没有任何理由必须使用 GCC。请解释为什么您必须使用 GCC,然后我们可能会为您提供进一步的帮助。

于 2013-07-28T10:15:19.030 回答