gpt4 book ai didi

c++ - 将问题与导出函数联系起来

转载 作者:行者123 更新时间:2023-11-28 06:21:29 25 4
gpt4 key购买 nike

我正在尝试创建一个基本的 hello world 程序,但在某些链接问题上失败了。

在程序.cpp中

#include <iostream>
#include <string>
#include "scanner.h"

using namespace std;

int main() {
string result = createScanner();
cout << result << endl;
return 0;
}

在扫描仪.h中

#include <string>

using namespace std;

string createScanner();

在扫描仪.cpp 中

#include <scanner.h>
#include <string>

using namespace std;

string createScanner() {
return "hello world";
}

使用此 CLI 方法:

clang++ -O3 -std=c++11 -stdlib=libc++  -I./includes/ -I./compiler/  compiler/program.cpp  -o hej

我得到这个错误:

Undefined symbols for architecture x86_64:
"createScanner()", referenced from:
_main in program-45fd7b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

最佳答案

选项 1:将 scanner.cpp 添加到命令行

clang++ -O3 -std=c++11 -stdlib=libc++  -I./includes/ -I./compiler/  compiler/program.cpp compiler/scanner.cpp -o hej

方案二:将编译步骤与链接步骤分开

clang++ -c -O3 -std=c++11 -stdlib=libc++  -I./includes/ -I./compiler/  compiler/program.cpp -o compiler/program.o
clang++ -c -O3 -std=c++11 -stdlib=libc++ -I./includes/ -I./compiler/ compiler/scanner.cpp -o compiler/scanner.o
clang++ -O3 -std=c++11 -stdlib=libc++ compiler/program.o compiler/scanner.o -o hej

选项 3:使用 Makefile

Makefile 的内容:

CXX=clang++
CXXFLAGS= -O3 -std=c++11 -stdlib=libc++ -Wall -I./includes/ -I./compiler/

hej: compiler/program.o compiler/scanner.o
clang++ -O3 -std=c++11 -stdlib=libc++ -o $@ $^

然后运行:

make

关于c++ - 将问题与导出函数联系起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com