编译和链接多个文件时使用g++

g++ when compile and link multiple files

本文关键字:g++ 文件 链接 编译      更新时间:2023-10-16

我遇到g++编译/链接问题。我想实现一个动态数组。我将类定义和实现分离为"DArray.h"answers"DArray.copp"文件。并在"DArraytest.cpp"文件中进行测试,该文件中包含一个主函数n。

所有这些文件都在同一目录中,我使用以下命令进行编译g++*.cpp

但它仍然给我这样的链接错误/tmp/cc4tu73o.o:在函数__static_initialization_and_destruction_0(int, int)': DArray.cpp:(.text+0x1d): undefined reference to std::ios_base::Init::Init()中'DArray.cpp:(.text+0x34):未定义对std::ios_base::Init::~Init()' /tmp/ccdHUZjJ.o: In function main的引用':DArraytest.cpp:(.text+0x12):对DArray<int>::DArray()' DArraytest.cpp:(.text+0x2e): undefined reference to DArray::append(int const&)'的未定义引用DArraytest.cpp:(.text+0x4a):对DArray<int>::append(int const&)' DArraytest.cpp:(.text+0x66): undefined reference to DArray::append(int const&)'的未定义引用DArraytest.cpp:(.text+0x82):对DArray<int>::append(int const&)' DArraytest.cpp:(.text+0xa3): undefined reference to std::cout'的未定义引用DArraytest.cpp:(.text+0xa8):对std::ostream::operator<<(int)' DArraytest.cpp:(.text+0xb0): undefined reference to std::basic_stream>&std::endl>(std::basic_stream>&)'DArraytest.cpp:(.text+0xb8):未定义对std::ostream::operator<<(std::ostream& (*)(std::ostream&))' DArraytest.cpp:(.text+0xc9): undefined reference to DArray::~DArray()'的引用DArraytest.cpp:(.text+0xeb):未定义对DArray<int>::~DArray()' /tmp/ccdHUZjJ.o: In function __static_initization_and_edestruction_0(int,int)'的引用:DArraytest.cpp:(.text+0x11d):对std::ios_base::Init::Init()' DArraytest.cpp:(.text+0x134): undefined reference to std::ios_base::Init::~Init()'的未定义引用/tmp/ccdHUZjJ.o:(.eh_frame+0x13):对"__gxx_personality_v0"的未定义引用collect2:错误:ld返回1退出状态

有人能帮我吗

我认为您需要向g++提供所有.cpp文件名。为了避免这种情况,您可以创建一个makefile,它可以自动将相同的编译规则应用于所有.cpp文件