C++ 编译错误 : ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

标签 c++ compiler-errors compilation

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

//测试.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

关于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/

相关文章:

C++, boost : which is fastest way to parse string like tcp://adr:port/into address string and one int for port?

c++ - Mutex 没有按预期工作

c - 为什么 CodeBlocks 不起作用?

C++ MSVC - 显示未捕获的异常消息

java - 编译器正在创建带有 $ 的额外类文件

linux - 为什么 "make all"似乎跳过了 "all:"规则?

c++ - 代码在 Sun Studio 上编译但在 gcc 上出错

c++ - 从单个集合生成所有子集

c++ - 即使我已经定义了所有功能,但在C++中仍获得对错误的 undefined reference

c - 所有括号匹配时输入末尾的语法错误