将问题与导出的函数相关联
Linking issues with an exported function
我试图创建一个基本的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
选项2:将编译步骤与链接步骤分离
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
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 关联容器的下界复杂性:成员函数与非成员函数
- 如何创建一个对象创建函数,该函数将由与其关联的名称调用?
- 构造函数是否unique_ptr初始化原始指针unique_ptr析构函数是否也删除关联的原始指针?
- 将整数(文字)与函数相关联,让呼叫者查询拖鞋的数量
- 如何在C++中创建将函数与参数相关联的 Map?
- 关联容器,比较函数不是元素类型的一部分吗?
- 函数类型定义可以与外部"C"相关联吗?
- 从函数返回变量地址时如何修复"与局部变量关联的堆栈内存地址"?
- MISRA C++规则 14-5-1:在与类型关联的命名空间中声明的泛型函数模板的名称
- 将 POSIX::open 函数关联到命名空间
- 在C 17中,为什么关联容器具有``擦除''成员函数,该函数会采用(non-`const`)``iTerator'
- std::async 和 lambda 函数在C++中没有给出关联的状态
- 将问题与导出的函数相关联
- 如何在给定函数之外获取与 C 闭包关联的(向上)值
- 如何将更改后的行与C代码的git存储库中的函数关联起来
- 在C++中,是否有一种将元数据与函数关联起来的整洁方法
- 为什么没有更简单的查找函数用于关联 std 容器
- 将文本字符串与函数关联