我是C新手,我只是想用Code :: Blocks编写一个控制台应用程序.这是(简化)代码:main.c:
#include <stdio.h>
#include <stdlib.h>
#include "test.c" // include not necessary for error in Code::Blocks
int main()
{
//t = test(); // calling of method also not necessary
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.c的:
void test() {}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建此程序时,它会出现以下错误:
*path*\test.c|1|multiple definition of `_ test'| obj\Debug\main.o:*path*\test.c|1|first defined here|
我没有办法多次定义测试(尽管我不知道下划线的来源)并且似乎不太可能将定义以某种方式包含两次.这是所有代码.
我已经排除了这个错误是由于某些命名冲突与其他函数或文件被称为test或test.c. 请注意,多个和第一个定义位于同一文件的同一行.
有谁知道造成这种情况的原因以及我能做些什么呢?谢谢!
例如:
code1.c/.cpp
int a;
// ... and so on
Run Code Online (Sandbox Code Playgroud)
code2.c/.cpp
int a;
int main(void) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
去编译:
$gcc code1.c code2.c # this is fine
$
$g++ code1.cpp code2.cpp # this is dead
/tmp/ccLY66HQ.o:(.bss+0x0): multiple definition of `a'
/tmp/ccnIOmPC.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
C&C++之间是否存在全局变量链接差异?
此函数是全局的,并在头文件中定义(暂时我想将其保留在那里).
头文件也构成一个具有内联函数的特定类,其中一个函数调用此全局函数.
源文件不包含任何有问题的全局函数.
有关错误原因的任何提示?
如果有人有兴趣,我可以发布代码.
mainwindow.o: In function `tileForCoordinate(double, double, int)':
mainwindow.cpp:(.text+0x310): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
moc_mainwindow.o: In function `qHash(QPoint const&)':
moc_mainwindow.cpp:(.text+0x0): multiple definition of `qHash(QPoint const&)'
main.o:main.cpp:(.text+0x0): first defined here
moc_mainwindow.o: In function `tileForCoordinate(double, double, int)':
moc_mainwindow.cpp:(.text+0x150): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
collect2: ld returned 1 exit status
make: *** [SimpleRouting] Error 1
Run Code Online (Sandbox Code Playgroud) 我有这些文件
consumer.cpp
consumer.hpp
defines.hpp
main.cpp
makefile
producer.cpp
producer.hpp
Run Code Online (Sandbox Code Playgroud)
这是define.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
Run Code Online (Sandbox Code Playgroud)
这个defined.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 …Run Code Online (Sandbox Code Playgroud) 因此,无论我做什么,我似乎都无法避免由于我在同一项目中的多个源代码文件中包含相同的头文件而使Dev C++发出大量多重定义错误.我非常希望避免将我的所有源代码转储到一个文件中,并且只包含一次标题,因为这将使我的文件很长并且难以管理.
从本质上讲,这是正在发生的事情:
#ifndef _myheader_h
#define _myheader_h
typedef struct MYSTRUCT{
int blah;
int blah2; } MYSTRUCT;
MYSTRUCT Job_Grunt;
MYSTRUCT *Grunt = &Job_Grunt;
MYSTRUCT Job_Uruk;
MYSTRUCT *Uruk = &Job_Grunt;
int Other_data[100];
void load_jobs();
#endif
Run Code Online (Sandbox Code Playgroud)
示例Cpp文件(它们几乎都看起来像这样):
#include "myheader.h"
void load_jobs(){
Grunt->blah = 1;
Grunt->blah2 = 14;
Uruk->blah = 2;
Uruk->blah2 = 15;
return; }
Run Code Online (Sandbox Code Playgroud)
请记住,我有大约5个包含这个标题的cpp文件,每个文件都处理头文件中找到的不同类型的结构.在这个例子中,当实际头文件中有大约4-6个不同的结构,并且有更多的成员时,只有一个结构包含几个成员.我在其中包含的所有文件都遵循您在此示例中看到的相同公式.
现在我明白了标头防护只能阻止每个单独的cpp文件多次包含头文件.似乎正在发生的事情是,当编译器在每个cpp的开头读取include时,它会再次定义头文件,这会导致它吐出以下行和行:
Multiple Definition of Uruk, first defined here
Multiple Definition of Job_Uruk, first defined here
Multiple Definition of Grunt, first defined here
Multiple Definition of Job_Grunt, first …Run Code Online (Sandbox Code Playgroud) 我有一个带有几个c和h文件的C程序.我决定将程序的一部分设置为"仅限标题",因此我将代码从c移到了h.现在我得到了多重定义问题,我不明白为什么.例如:
main.c includes utils.h
vector.c includes utils.h
Run Code Online (Sandbox Code Playgroud)
我将utils.c中的所有内容移动到utils.h(当然从项目中删除了utils.c).utils.h以.开头
#ifndef UTILS_H_
#define UTILS_H_
// and end with:
#endif
Run Code Online (Sandbox Code Playgroud)
为了确保我的后卫是独一无二的,我尝试改变它(例如:UTILS718171_H_),但它不起作用.
仍然,编译器抱怨:
/tmp/ccOE6i1l.o: In function `compare_int':
ivector.c:(.text+0x0): multiple definition of `compare_int'
/tmp/ccwjCVGi.o:main.c:(.text+0x660): first defined here
/tmp/ccOE6i1l.o: In function `compare_int2':
ivector.c:(.text+0x20): multiple definition of `compare_int2'
/tmp/ccwjCVGi.o:main.c:(.text+0x6e0): first defined here
/tmp/ccOE6i1l.o: In function `matrix_alloc':
ivector.c:(.text+0x40): multiple definition of `matrix_alloc'
/tmp/ccwjCVGi.o:main.c:(.text+0x0): first defined here
...
Run Code Online (Sandbox Code Playgroud)
问题可能是这样的:所有c文件都被编译并获得自己的代码版本,然后在链接时会导致问题,但我老实说不知道如何解决这个问题.
我有3个项目:服务器,客户端和共享.在Commons中创建头和源对不会导致任何问题,我可以从服务器和客户端自由访问这些功能.
然而,由于某些原因使中附加源/头文件服务器或客户端的项目总是会引起multiple definition of (...)和first defined here错误.
例:
commands.h(在Client项目的根目录中)
#ifndef COMMANDS_H_
#define COMMANDS_H_
#include "commands.c"
void f123();
#endif /* COMMANDS_H_ */
Run Code Online (Sandbox Code Playgroud)
commands.c(在Client项目的根目录中)
void f123(){
}
Run Code Online (Sandbox Code Playgroud)
main.c(在Client项目的根目录中)
#include "commands.h"
int main(int argc, char** argv){
}
Run Code Online (Sandbox Code Playgroud)
错误:
make: *** [Client] Error 1 Client
first defined here Client
multiple definition of `f123' commands.c
Run Code Online (Sandbox Code Playgroud)
清洁,重建索引,重建项目没有帮助.也没有重新启动计算机.
我想写一个要使用的库,你只需要包含一个头文件.但是,如果您有多个源文件并在两者中都包含标头,则会出现多个定义错误,因为该标头都是在标头中声明和定义的.我认为在Boost中我看过只有头文件的库.他们是怎么做到的?
我正在尝试使用MinGW编译器在Qt中使用C++创建一个简单的GUI应用程序(到目前为止).然而,编译器,通知我有一个multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)'对line 4的wiimotescouter.cpp.我正在使用检查以确保标题不会被多次包含,但显然它不起作用,我不知道为什么.
这是头文件:
#ifndef WIIMOTESCOUTER_H
#define WIIMOTESCOUTER_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class WiimoteScouter : public QWidget
{
Q_OBJECT
public:
WiimoteScouter(QWidget *parent = 0);
private:
QLineEdit *eventLine;
};
#endif // WIIMOTESCOUTER_H
Run Code Online (Sandbox Code Playgroud)
这是cpp文件:
#include <QtGui>
#include "wiimotescouter.h"
WiimoteScouter::WiimoteScouter(QWidget *parent) :
QWidget(parent)
{
QLabel *eventLabel = new QLabel(tr("Event:"));
eventLine = new QLineEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(eventLabel, 0, 0);
mainLayout->addWidget(eventLine, 0, 1);
setLayout(mainLayout);
setWindowTitle(tr("Wiimote Alliance Scouter"));
}
Run Code Online (Sandbox Code Playgroud)
最后,main.cpp:
#include …Run Code Online (Sandbox Code Playgroud) 这是我的.pro文件:
QT += core gui widgets
TARGET = link_mult_def
TEMPLATE = app
SOURCES += main.cpp \
path2/file.cpp \
path1/file.cpp
HEADERS +=
Run Code Online (Sandbox Code Playgroud)
出于某种原因,QtCreator在从.cpp文件构建.o文件时不尊重源文件夹结构.这两个文件都将编译为"shadow_build_directory/file.o".我希望构建过程在shadow构建目录中创建path1和path2目录,并将"path1/file.cpp"编译为"shadow_build_directory/path1/file.o",将"path2/file.cpp"编译为"shadow_build_directory/path2 /" file.o".
由于来自两个来源的编译符号在文件中加起来,因此它还不是一个大问题.当QtCreator尝试链接时,它成为一个大问题:
g++ -Wl,-O1 -o link_mult_def main.o file.o file.o -L/usr/lib/x86_64-linux-gnu -lQtCore -lpthread
Run Code Online (Sandbox Code Playgroud)
QtCreator链接file.o两次,这使得链接器失败并出现多个定义错误.
如何确保QtCreator编译为反映源目录结构的目标文件?
谢谢
编辑:
路径1/file.cpp
#include <iostream>
void function1()
{
std::cout << "function1" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
路径2/file.cpp
#include <iostream>
void function2()
{
std::cout << "function2" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
QtCreator的构建过程:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../link_mult_def -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui …Run Code Online (Sandbox Code Playgroud)