建筑x86_64的未定义符号 ->建筑x86_64找不到的符号

Undefined symbols for architecture x86_64 -> symbol(s) not found for architecture x86_64

本文关键字:建筑 符号 x86 找不到 gt 未定义      更新时间:2023-10-16

我正在学习c ,我已经掌握了这一点:

helloworld.cpp

#include <iostream>
using namespace std;
int main() {
   cout << "Hi" << endl;
   return 0;
}

我正在使用Macos Mojave,用于编译,我使用命令

>> g++ helloworld.cpp
>> ./a.out

如果工作正常。现在我想使用标题文件。因此,我已经创建了以下文件:

test.cpp

#include <iostream>
#include "add.h"
using namespace std;
int main() {
   add(4,7);
   return 0;
}

add.h

#pragma once
int add(int a, int b);

add.cpp

#include "add.h"
int add(int a, int b) {
    return a + b;
}

当我尝试编译时,我会得到:

>> g++ test.cpp
Undefined symbols for architecture x86_64:
  "add(int, int)", referenced from:
      _main in test-ebc106.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人知道如何解决这个问题吗?

g++ test.cpp add.cpp

需要编译每个CPP文件以分开.OBJ文件