C++ 函数的多重定义,为什么是';警卫没抓住这个吗?

C++ 函数的多重定义,为什么是';警卫没抓住这个吗?,c++,include,makefile,C++,Include,Makefile,因此,我在文件cerus.h中编写了一小部分日志函数。该文件的内容如下所示。它包含在main.cpp、model.cpp、engine.cpp和camera.cpp中。如图所示,我包含了防护装置,因此我不确定为什么会出现此错误: 输出$make jed@ArchPC:~/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.

因此,我在文件
cerus.h
中编写了一小部分日志函数。该文件的内容如下所示。它包含在
main.cpp
model.cpp
engine.cpp
camera.cpp
中。如图所示,我包含了防护装置,因此我不确定为什么会出现此错误:

输出
$make

jed@ArchPC:~/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
如果有人能向我解释为什么我的警卫没有抓到这个,我将非常感谢你的帮助


EDIT:通过添加一个名为
cerus.cpp
的文件并在那里定义我的日志函数,而不是在
cerus.h
中,解决了这个问题。这种类型的保护是为了避免在相同的翻译单元中声明或定义内容

它不会对不同翻译单位中的多个定义产生任何影响(即多个源文件)


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

守卫没有做错什么,他们只是保护您的声明/内联线/模板。
定义才是真正的问题。若您的cpp中有内联函数,请将它们放在标题中,对于模板也是如此。不要包含cpp文件。无法看到大部分代码,但大多数情况下都是这样

我可以使用什么样的保护来保护不同源文件中的内容?@JStevens根本不在头文件中定义内容?申报就行了。或者,将这些函数声明为
static
inline
@JStevens,即使没有这样的保护。如果这些函数在标题中,则应将其声明为
inline
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