簡體   English   中英

架構 x86_64 的未定義符號:linker 錯誤

[英]Undefined symbols for architecture x86_64: linker error

我正在嘗試對 cpp 文件的基本鏈接進行測試,我一直在搜索並且在尋找解決方案時遇到了很多麻煩。 我知道我必須在兩個 cpp 中都包含 header,但是我在嘗試將這兩者一起運行時遇到了麻煩。

//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++ 程序時指定所有翻譯單元文件。

您的程序包含兩個源文件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 有助於自動執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM