Multiple definition of first defined here gcc(此处首先定义的多重定义 gcc)
问题描述
我有这些文件
consumer.cpp
consumer.hpp
defines.hpp
main.cpp
makefile
producer.cpp
producer.hpp
这是文件defines.hpp
here's the file 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
这个defines.hpp 文件包含在producer.hpp 和consumer.hpp 中.producer.hpp 和consumer.hpp 文件分别包含在producer.cpp 和consumer.cpp 和main.cpp 中.编译时出现错误.
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.
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
这是我的生成文件
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
如何解决这个问题?
推荐答案
在 C++(以及 C)中,声明和定义是有区别的,比如变量.您在头文件中所做的是定义变量,这意味着包含头文件的每个源文件都将具有定义.
In C++ (as well as in C) there is a difference between declaring and defining things like variables. What you are doing in the header file is defining the variables, which means that every source file that includes the header file will have the definitions.
在头文件中你应该只声明变量,然后在单个源文件中定义它们.
In the header file you should only declare the variables, and then in a single source file define them.
所以在头文件中做例如
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).
这篇关于此处首先定义的多重定义 gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:此处首先定义的多重定义 gcc
- Easyx实现扫雷游戏 2023-02-06
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言详解float类型在内存中的存储方式 2023-03-27
- Qt计时器使用方法详解 2023-05-30
- C语言qsort()函数的使用方法详解 2023-04-26
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- ubuntu下C/C++获取剩余内存 2023-09-18