c++ - 在 Linux 上编译 C++ 代码。需要使用 gcc/4.7.2。找不到标准库

标签 c++ gcc standard-library gcc4.7

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

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

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

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

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

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

我的 makefile 包含和编译器选项如下。我最初是在我的 Mac 上使用 QT 创建的。我修改它以在 gcc/4.7.2 上与 Linux 集群一起工作。所以完全有可能有点乱。

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

module load gcc/4.7.2

那么makefile是:

CC = gcc

DEFINES = -DIPMGEMPLUGIN -DNOPARTICLEARRAY -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 TARGET = mylib.a

.SUFFIXES: .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 "$@" "$<"

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

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

现在我遇到的错误种类:

Code.o: In function _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.part.8': Code.cpp:(.text+0x12): undefined reference tostd::basic_ios >::clear(std::_Ios_Iostate)' Code.o: In function _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.constprop.101': Code.cpp:(.text+0x35): undefined reference tostd::cout' Code.cpp:(.text+0x3a): undefined reference to `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): undefined reference to 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): undefined reference to vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x174): undefined reference to vtable for std::basic_stringstream, std::allocator >' Code.cpp:(.text+0x17c): undefined reference to vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x184): undefined reference to vtable for std::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): In function Other::free_internal()': gl3.cpp:(.text+0x251): undefined reference tooperator delete' gl3.cpp:(.text+0x262): ...

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

最佳答案

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

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

关于c++ - 在 Linux 上编译 C++ 代码。需要使用 gcc/4.7.2。找不到标准库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760378/

相关文章:

c++ - 在 C++ 中将十六进制转换为字节

c++ - 用于 WinRT 的 zlib?

c - 为什么标签的第一部分应该是声明?为什么不声明?

gcc - 如何注册 GIMPLE_PASS?

abstract - Agda 标准库 - 为什么更多属性没有标记为抽象?

ios - 我在哪里可以找到 libsystem_c.dylib 的源代码?

映射函数的 C++ 模拟

c++ - 使用指针创建静态数组?

c++ - 搜索继承另一个类的 C++ 类

linux - 构建 glibc 时出现 "can not be used when making a shared object"