C++ 多重定义首先在这里定义gcc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19675685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:02:37  来源:igfitidea点击:

Multiple definition of first defined here gcc

c++gcclinkerincludemultiple-definition-error

提问by Some programmer dude

I have these files

我有这些文件

consumer.cpp
consumer.hpp
defines.hpp
main.cpp
makefile
producer.cpp
producer.hpp

here's the file defines.hpp

这是文件defines.hpp

#ifndef DEFINES_HPP
#define DEFINES_HPP

#include <cassert>
#include <pthread.h> 
#include <queue>
#include <stdlib.h>
#include <string>
#include <unistd.h>

pthread_mutex_t set_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var = PTHREAD_COND_INITIALIZER;

std::queue<int> q;

#endif // DEFINES_HPP

This defines.hpp file is included in the producer.hpp and consumer.hpp. producer.hpp and consumer.hpp files respectively included to producer.cpp and consumer.cpp and yet to main.cpp. When compiling I get an error.

这个defines.hpp 文件包含在producer.hpp 和consumer.hpp 中。producer.hpp 和consumer.hpp 文件分别包含在producer.cpp 和consumer.cpp 中,但尚未包含在main.cpp 中。编译时出现错误。

g++ -o main producer.cpp consumer.cpp main.cpp producer.hpp consumer.hpp defines.hpp -lpthread -ggdb
/tmp/ccctuRp7.o: In function `__gnu_cxx::new_allocator<int>::destroy(int*)':
/home/vardan/test/consumer.cpp:8: multiple definition of `set_queue_mutex'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:8: first defined here
/tmp/ccctuRp7.o: In function `std::deque<int, std::allocator<int> >::front()':
/home/vardan/test/consumer.cpp:12: multiple definition of `condition_var'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:11: first defined here
/tmp/ccctuRp7.o: In function `global::consumer::entry_point(void*)':
/home/vardan/test/consumer.cpp:17: multiple definition of `q'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:17: first defined here
/tmp/ccKCxptM.o: In function `main':
/home/vardan/test/main.cpp:8: multiple definition of `set_queue_mutex'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:8: first defined here
/tmp/ccKCxptM.o: In function `main':
/home/vardan/test/main.cpp:13: multiple definition of `condition_var'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:11: first defined here
/tmp/ccKCxptM.o: In function `main':
/home/vardan/test/main.cpp:25: multiple definition of `q'
/tmp/cciGccft.o:/home/vardan/test/producer.cpp:17: first defined here
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

Here is my makefile

这是我的makefile

main : producer.cpp consumer.cpp main.cpp producer.hpp consumer.hpp defines.hpp
    g++ -o $@ $^ -lpthread -ggdb
.PHONY : clean
clean:
    rm -rf main *.swf *.o

How to solve this problem ?

如何解决这个问题呢 ?

回答by Some programmer dude

In C++ (as well as in C) there is a difference between declaringand definingthings like variables. What you are doing in the header file is definingthe variables, which means that every source file that includes the header file will have the definitions.

在 C++(以及 C)中,声明定义诸如变量之类的东西是有区别的。您在头文件中所做的是定义变量,这意味着每个包含头文件的源文件都将具有定义。

In the header file you should only declarethe variables, and then in a single source file define them.

在头文件中你应该只声明变量,然后在一个源文件中定义它们。

So in the header file do e.g.

所以在头文件中做例如

extern pthread_mutex_t set_queue_mutex;
extern pthread_cond_t  condition_var;

extern std::queue<int> q;

And then in a single source file put the definitions (what you have now).

然后在单个源文件中放置定义(您现在拥有的)。