2015-11-26 74 views
0

所以我在文件cerus.h中写了一小组记录函数。该文件的内容可以在下面看到。它包含在main.cpp,model.cpp,engine.cppcamera.cpp中。可以看出,我已经包括后卫,所以我不知道为什么我收到此错误:函数的多重定义,为什么警卫没有捕捉到这个?

输出的

$ make

[email protected]:~/glPlayground$ make 
g++ -std=c++11 -c model.cpp -o bin/model.o 
g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o 
g++ -std=c++11 -c camera.cpp -o bin/camera.o 
g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw 
bin/engine.o: In function `LOG(char const*)': 
engine.cpp:(.text+0x0): multiple definition of `LOG(char const*)' 
bin/main.o:main.cpp:(.text+0x0): first defined here 
bin/engine.o: In function `LOGERR(char const*)': 
engine.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)' 
bin/main.o:main.cpp:(.text+0x3d): first defined here 
bin/model.o: In function `LOG(char const*)': 
model.cpp:(.text+0x0): multiple definition of `LOG(char const*)' 
bin/main.o:main.cpp:(.text+0x0): first defined here 
bin/model.o: In function `LOGERR(char const*)': 
model.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)' 
bin/main.o:main.cpp:(.text+0x3d): first defined here 
bin/camera.o: In function `LOG(char const*)': 
camera.cpp:(.text+0x0): multiple definition of `LOG(char const*)' 
bin/main.o:main.cpp:(.text+0x0): first defined here 
bin/camera.o: In function `LOGERR(char const*)': 
camera.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)' 
bin/main.o:main.cpp:(.text+0x3d): first defined here 
collect2: error: ld returned 1 exit status 
Makefile:4: recipe for target 'main' failed 
make: *** [main] Error 1 

cerus.h

#ifndef CERUS_H 
#define CERUS_H 
#include <iostream> 
//Need to add Windows and Mac Includes here 

// Linux Include Statements 

void LOG(const char* str){ 
    std::cout << "[INFO]" << str << "\n"; 
} 
void LOGERR(const char* str){ 
    std::cout << "[ERROR]" << str << "\n"; 
} 

#include <GL/glew.h> 
#include <GL/gl.h> 
#include <GL/glu.h> 
#include <GL/glut.h> 

#include <GLFW/glfw3.h> 

#endif 

Makefile

all: main 

main: bin/main.o bin/engine.o bin/model.o bin/tinyobj.o bin/camera.o cerus.h 
    g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw 

bin/main.o: main.cpp cerus.h 
    g++ -std=c++11 -c main.cpp -o bin/main.o 

bin/engine.o: engine.cpp engine.h cerus.h 
    g++ -std=c++11 -c engine.cpp -o bin/engine.o 

bin/tinyobj.o: tiny_obj_loader.cc tiny_obj_loader.h cerus.h 
    g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o 

bin/model.o: model.cpp model.h cerus.h 
    g++ -std=c++11 -c model.cpp -o bin/model.o 

bin/camera.o: camera.cpp camera.h cerus.h 
    g++ -std=c++11 -c camera.cpp -o bin/camera.o 

clean: 
    rm -f bin/*.o main 

如果有人能向我解释为什么我的警卫没有抓住这一点,我将非常感谢帮助。

编辑:通过添加一个名为cerus.cpp文件和定义,而不是在cerus.h

+0

如果它们在标题中,你应该声明这个函数为'inline' – user463035818

回答

2

这种类型的后卫是为了避免被宣布或同一转换单元定义的东西我的日志功能有修正这个问题。

它不会对不同翻译单元(即多个源文件)中的多重定义有任何影响。

在这种情况下,您应该将函数LOGLOGERR的定义移动到另一个.cpp文件,并将函数声明放在头文件中。

+0

我可以使用什么样的警戒来保护不同源文件中的内容? – JStevens

+2

@JStevens只是不*在头文件中定义*的东西?只需*申报*他们。或者,将这些函数声明为“static”或“inline”。 –

+0

@JStevens没有这样的防范。 – juanchopanza

0

卫兵没有做错任何事,他们只是保护你的声明/内联/模板。 这是真正的问题的定义。如果你的cpp中有内联函数,把它们放在标题中,模板也一样。不要包含cpp文件。看不到很多你的代码,但这是大多数情况。

相关问题