【发布时间】: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)
【问题讨论】:
-
这能回答你的问题吗? What is an undefined reference/unresolved external symbol error and how do I fix it?你没有编译function.cpp
-
特别是this answer。
-
你应该编译所有的 cpp 文件,而不仅仅是 testMain.cpp,例如
g++ testMain.cpp functions.cpp -o testMain
标签: c++ compiler-errors