有人可以解释一下我在做什么错吗?我正在使用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;
};

//test.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和google,但仍然无法弄清楚自己做错了什么。非常感谢

最佳答案

您没有与test.o链接。

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

或者您可以一次完成两个操作:
g++ maintest.cpp test.cpp -o mainTest

关于c++ - C++编译错误: ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48526811/

10-11 17:06