我有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请帮帮忙!
发布于 2013-01-15 11:37:51
您必须指定定义censorship的文件。
g++ main.cpp censorship_mng.cpp -o main发布于 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
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}并从void censorship()文件中删除censorship_mng.cpp。
发布于 2013-01-15 11:49:49
一旦您的项目开始使用多个源文件编译成一个二进制文件,手动编译就会变得乏味。
这通常是您开始使用构建系统(如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)https://stackoverflow.com/questions/14336852
复制相似问题