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

时间:2018-01-30 17:03:05

标签: c++ compiler-errors compilation

有人可以解释我在这里做错了什么吗?我正在使用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和谷歌,但我仍然无法弄清楚我做错了什么。非常感谢

1 个答案:

答案 0 :(得分:1)

您未与test.o链接。

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

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

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