C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v



有人可以解释一下我在这里做错了什么吗?我正在使用 macOS以下是代码:

/

/test.h

#include <iostream>
#include <string>
class Test 
{
public:
    Test (std::string, std::string, int);
private:
    std::string par1;
    std::string par2;
    int par3;
};

测试.cpp

#include <string>
#include <string>
#include "test.h"
using namespace std;
Test::Test (string first, string second, int third)
{
    par1 = first;
    par2 = second;
    par3 = third;
}
/

/mainTest.cpp

#include <string>
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
    Test test1 ("A", "B", 2);
}

我用来编译的命令是:

g++ mainTest.cpp -o mainTest

这是我得到的错误:

Undefined symbols for architecture x86_64:
  "Test::Test(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
      _main in mainTest-056a8f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已经搜索了stackoverflow和谷歌,但我仍然无法弄清楚我做错了什么。多谢

您没有链接到test.o

g++ -c test.cpp -o test.o
g++ mainTest.cpp test.o -o mainTest

或者您可以同时执行这两项操作:

g++ maintest.cpp test.cpp -o mainTest

最新更新