首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:未定义的对“审查()”的引用

错误:未定义的对“审查()”的引用
EN

Stack Overflow用户
提问于 2013-01-15 11:35:15
回答 3查看 401关注 0票数 0

可能重复: 什么是未定义的引用/未解决的外部符号错误,以及如何修复它?

我有main.cpp

代码语言:javascript
复制
#include "censorship_dec.h"

using namespace std;

int main () {
    censorship();
    return 0;
}

这是我的censorship_dec.h

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

void censorship();

这是我的censorship_mng.cpp

代码语言:javascript
复制
#include "censorship_dec.h"
using namespace std;

void censorship()
{
   cout << "bla bla bla" << endl;
}

我试图在SSH (Linux)中运行这些文件,所以我写了:make main,但是我得到了:

代码语言:javascript
复制
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

请帮帮忙!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-15 11:37:51

您必须指定定义censorship的文件。

代码语言:javascript
复制
g++ main.cpp censorship_mng.cpp -o main
票数 5
EN

Stack Overflow用户

发布于 2013-01-15 11:37:54

必须在编译命令中添加censorship_mng.cpp

g++ main.cpp censorship_mng.cpp -o main

另一个解决方案(如果您真的不想更改您的编译命令)是将void censorship();变成一个inline函数,并将它从.cpp转移到.h

censorship_dec.h

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

inline void censorship()
{
  // your code
}

并从void censorship()文件中删除censorship_mng.cpp

票数 3
EN

Stack Overflow用户

发布于 2013-01-15 11:49:49

一旦您的项目开始使用多个源文件编译成一个二进制文件,手动编译就会变得乏味。

这通常是您开始使用构建系统(如Makefile )的时候。

使用默认构建规则的非常简单的Makefile可能如下所示

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14336852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档