【问题标题】:Undefined symbols for architecture x86_64: linker error架构 x86_64 的未定义符号:链接器错误
【发布时间】:2020-05-23 08:05:04
【问题描述】:

我正在尝试对 cpp 文件的基本链接进行测试,我一直在搜索,但在寻找解决方案时遇到了很多麻烦。我知道我必须在两个 cpp 中都包含标头,但是我在尝试将这两个一起运行时遇到了麻烦。

//testMain.cpp

#include <iostream>
#include <stdio.h>
#include "func.h"

using namespace Temp;

int main()
{
    getInfo();
    return 0;
}
//func.h

#ifndef FUNC_H
#define FUNC_H

#include <iostream>
#include <stdio.h>


namespace Temp{
int getInfo();
}


#endif
//functions.cpp
#include "func.h"

using namespace std;

int Temp::getInfo()
{

    return 5 + 6;
}
//error that I'm getting using VS Code
cd "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/" && g++ testMain.cpp -o testMain && "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/"testMain
Undefined symbols for architecture x86_64:
  "Temp::getInfo()", referenced from:
      _main in testMain-1f71a1.o
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


【解决方案1】:

您应该在链接 C++ 程序时指定所有翻译单元文件。

您的程序包含两个源文件,testMain.cppfunctions.cpp

因此 compile-and-link 命令应该类似于:

g++ testMain.cpp functions.cpp -o testMain

或者,您可以分别编译每个源代码,然后将它们链接成可执行文件:

g++ -c testMain.cpp -o testMain.o
g++ -c functions.cpp -o functions.o
g++ testMain.o functions.o -o testMain

拥有某种 Makefile 有助于自动执行此操作。

【讨论】:

  • 编译成功!!!谢谢,之后我将如何在 VS Code 中执行所有操作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 2017-02-15
  • 1970-01-01
  • 2019-12-05
  • 2023-03-23
相关资源
最近更新 更多