gpt4 book ai didi

c++ - 函数的多重定义,为什么守卫没有捕获这个?

转载 作者:行者123 更新时间:2023-11-28 02:16:17 26 4
gpt4 key购买 nike

所以我在文件 cerus.h 中写了一小组日志记录函数。该文件的内容如下所示。它包含在 main.cppmodel.cppengine.cppcamera.cpp 中。可以看出,我包含了 guard ,所以我不确定为什么会收到此错误:

$ 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

生成文件

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

如果有人能向我解释为什么我的 guard 没有发现这个,我将不胜感激。

编辑: 通过添加一个名为 cerus.cpp 的文件并在其中而不是在 cerus.h 中定义我的日志记录函数来解决此问题/p>

最佳答案

这种守卫是为了避免在相同的翻译单元中声明或定义事物。

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

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

关于c++ - 函数的多重定义,为什么守卫没有捕获这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com