错误:未定义对"审查()"的引用
Error: undefined reference to `censorship()'
可能重复:
什么是未定义的引用/未解决的外部符号错误,如何修复?
我有main.cpp
:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
这是我的censorship_dec.h
:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
这是我的censorship_mng.cpp
:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
我试着在SSH(Linux(中运行这些文件,所以我写了:make main
,但我得到了:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
请帮忙!
您必须指定定义censorship
的文件。
g++ main.cpp censorship_mng.cpp -o main
您必须在编译命令中添加censorship_mng.cpp
:
g++main.cpp审查_mng.cpp-o主
另一个解决方案(如果您真的不想更改编译命令(是将void censorship();
转换为inline
函数,并将其从.cpp
移动到.h
。
censorship_dec.h
:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
并从censorship_mng.cpp
文件中删除void censorship()
。
一旦项目开始使用几个源文件编译成一个二进制文件,手动编译就会变得乏味。
这通常是您开始使用构建系统的时候,例如Makefile
使用默认构建规则的非常简单的Makefile可能看起来像
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 错误:未定义对"审查()"的引用