The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
The content of commands.h after the pre-processor phase looks like this:
#ifndef COMMANDS_H_
#define COMMANDS_H_
// function definition
void f123(){
}
// function declaration
void f123();
#endif /* COMMANDS_H_ */
This is an error because you can’t declare a function after its definition in C. If you swapped #include "commands.c" and the function declaration the error shouldn’t happen because, now, the function prototype comes before the function declaration.
However, including a .c file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h in commands.c and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123(); // function declaration
#endif
commands.c
#include "commands.h"
void f123(){} // function definition
Recommended Answers
It’s a shot in the dark, but if you include a .h or .cpp file more than once and don’t do something like this:
#ifndef ... #define ... // code #endifyou could get that error. I’m not saying that is the cause here, but it’s something …
Jump to Post
Move the definition of QStringList FuncCntrlParams::type_pairPotList into the respective .cpp file.
Jump to Post
All 7 Replies

VernonDozier
2,218
Posting Expert
Featured Poster
14 Years Ago
It’s a shot in the dark, but if you include a .h or .cpp file more than once and don’t do something like this:
#ifndef ...
#define ...
// code
#endif
you could get that error. I’m not saying that is the cause here, but it’s something to consider.

Kob0724
20
Junior Poster in Training
14 Years Ago
Yea, I already have those in the .h file. I tried something new though. I took the reference to type_pairPotList out of interpotcntrlparams.cpp and I still got the error. This time it was this:
debug/mymetawindow.o: In function `qt_noop()’:
/home/f07/xxx/QT/include/QtCore/qglobal.h:1425: multiple definition of `FuncCntrlParams::type_pairPotList’
make[1]: Leaving directory `/net/home/f07/xxx/workspace/tramontoGUI’
debug/funccntrlparams.o:/home/f07/xxxx/QT/include/QtCore/qglobal.h:1425: first defined here
debug/moc_funccntrlparams.o: In function `qt_noop()’:
/home/f07/xxx/QT/include/QtCore/qglobal.h:1425: multiple definition of `FuncCntrlParams::type_pairPotList’
debug/funccntrlparams.o:/home/f07/xxx/QT/include/QtCore/qglobal.h:1425: first defined here
So it would seem like the problem is squarely located in my funcctnrlparams.h file. Here’s that file with a little more detail:
#ifndef FUNCCNTRLPARAMS_H_
#define FUNCCNTRLPARAMS_H_
#include <QWidget>
#include <QStringList>
class FuncCntrlParams : public QWidget
{
Q_OBJECT
public:
/**
* Constructs a new FuncCntrlParams QWidget.
*/
FuncCntrlParams(QWidget *parent=0);
static QStringList type_pairPotList;
};
QStringList FuncCntrlParams::type_pairPotList = QStringList() << "Pair LJ12-6 CS" << "Pair Coulomb CS" << "Pair Coulomb" << "Pair Yukawa CS";
#endif /*FUNCCNTRLPARAMS_H_*/

14 Years Ago
Move the definition of QStringList FuncCntrlParams::type_pairPotList into the respective .cpp file.

Kob0724
20
Junior Poster in Training
14 Years Ago
I can’t do that. I have to define it in the header file or else I get this error:
net/home/f07/xxx/workspace/tramontoGUI/interpotcntrlparams.cpp:48: undefined reference to `FuncCntrlParams::type_pairPotList’

14 Years Ago
I can’t do that. I have to define it in the header file or else I get this error:
You need to #include the respective header file in interpotcntrlparams.cpp.

Kob0724
20
Junior Poster in Training
14 Years Ago
You need to #include the respective header file in interpotcntrlparams.cpp.
Yea I did do that.
interpotcntrlparams.cpp
//interpotcntrlparams.cpp
#include "interpotcntrlparams.h"
#include "surfcntrlparams.h"
#include "funccntrlparams.h"
.
.
.
And I still get the error:
net/home/f07/xxx/workspace/tramontoGUI/interpotcntrlparams.cpp:48: undefined reference to `FuncCntrlParams::type_pairPotList’

Kob0724
20
Junior Poster in Training
14 Years Ago
I figured it out. The problem was that I was defining type_pairPotList in the constructor of FuncCntrlParams. Like this:
//funcntrlparams.cpp
.
.
FuncCntrlParams::FuncCntrlParams(QWidget *parent)
:QWidget(parent)
{
QStringList type_pairPotList = QStringList() << "Pair LJ12-6 CS" << "Pair Coulomb CS" << "Pair Coulomb" << "Pair Yukawa CS";
.
.
}
.
.
Once I took it out of the constructor like this:
//funcntrlparams.cpp
.
.
QStringList type_pairPotList = QStringList() << "Pair LJ12-6 CS" << "Pair Coulomb CS" << "Pair Coulomb" << "Pair Yukawa CS";
FuncCntrlParams::FuncCntrlParams(QWidget *parent)
:QWidget(parent)
{
.
.
}
.
.
Everything worked.
Thanks for bearing with me mitrmkar. I’m still transitioning from Java and I think because of my Java mindset I figured since I’d already declared the variable outside of the constructor, the only place to define it now would be in the constructor.
Reply to this topic
Be a part of the DaniWeb community
We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.
Друзья, что это за ошибка?
Ниже присказка: «collect2: ld returned 1 exit status»
читал. Пишут. что из-за повторных #include-ов. Но не пойму, где я напортачил
Среда QT Creator
Проект — dll. При создании автоматически средой был создан конструктор:
| C++ | ||
|
и реализация:
| C++ | ||
|
на нее эта ошибка указывает
В принципе они мне не нужны. Мне там нечего инициализировать. Я просто экспортирую extern функции. В этом весь смысл.
Повторных #include у себя не вижу. Здесь находил, что это может быть из-за using namespace в заголовочном файле. Перебрал все в *.cpp-шные. Впрочем судите сами:
alloc.h:
| C++ | ||
|
main.h
| C++ | ||
|
alloc.cpp
| C++ | ||
|
main.cpp
| C++ | ||
|
PS: так же в каждом заголовочном на вс случай везде поставил #pragme once
В ссылке на стек есть решение, обозначить функции как inline. Я попробовал, действительно, на эту функцию не ругается. Но ругается на следующую
. Я думаю, это не есть нормально
Добавлено через 43 минуты
Блин,я думал, что нашел, что ошибку…
У меня в «alloc.h» была структура и класс:
| C++ | ||
|
В alloc.cpp реализация:
| C++ | ||
|
Как только заменил sizeof(jmp_near) на sizeof(1) все сразу заработало
Но для меня эта ошибка не понятна, т.к. в основном коде я постоянно использую конструкцию sizeof(jmp_near), например, скопировав тело jmp_near в основной код
| C++ | ||
|
И все прекрасно компилировалось и компилируется.
Добавлено через 3 минуты
Короче, только я добавил этот коммент и все перестало компилироваться 
Я просто написал одну строку cout << … в основном коде и удалил ее. Откомпилировалось только тогда когда я удалил весь код из alloc.cpp.
Когда же я добавил в alloc.cpp только:
| C++ | ||
|
то он отказался компилироваться и выдал ту же ошибку. Что за фигня?
Добавлено через 12 минут
Теперь он компилируется при:
alloc.h
| C++ | ||
|
и
alloc.cpp
| C++ | ||
|
Но отказывается, если я добавлю в alloc.h
| C++ | ||
|
Ничего не пойму. Компилятор мне таким образом запрещает объявить глобальную переменную. Это нормально?
Добавлено через 56 секунд
PS: пересборка не помогает. Смириться?
So I’m quite certain that this should be an easy question but I’ve been working on it for too long and can’t figure it out so I’ll see if anyone else can figure out what I’m overlooking. From everything I’ve seen online, the root of my issue should be having too many #include .h but can’t seem to figure out where I went wrong. Here are my files:
//term.h
#ifndef TERM_H
#define TERM_H
#include <string>
class Term { /* code … */
//term.cpp
#ifndef TERM_CPP
#define TERM_CPP
#include «term.h»
#include <iostream>
//code …
//SortingList.h (template file, no included .h files, I seriously doubt my problem is from here)
//autocomplete.h
#ifndef AUTOCOMPLETE_H
#define AUTOCOMPLETE_H
#include «term.h»
#include «SortingList.h»
//code….
//autocomplete.cpp
#ifndef AUTOCOMPLETE_CPP
#define AUTOCOMPLETE_CPP
#include «autocomplete.h»
//code…
//main.cpp
#ifndef MAIN_CPP
#define MAIN_CPP
#include «autocomplete.h»
#include <fstream>
#include <sstream>
//code…
//the actual error message g++ -c autocomplete.cpp -o autocomplete.o g++ autocomplete.o term.o main.o SortingList.o -o CS216PA3 term.o: In function Term::Term()': term.cpp:(.text+0x0): multiple definition ofTerm::Term()’ autocomplete.o:autocomplete.cpp:(.text+0x0): first defined here term.o: In function Term::Term()': term.cpp:(.text+0x0): multiple definition ofTerm::Term()’ autocomplete.o:autocomplete.cpp:(.text+0x0): first defined here term.o: In function Term::Term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long)': term.cpp:(.text+0x5a): multiple definition ofTerm::Term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long)’ autocomplete.o:autocomplete.cpp:(.text+0x5a): first defined here term.o: In function Term::Term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long)': term.cpp:(.text+0x5a): multiple definition ofTerm::Term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long)’ autocomplete.o:autocomplete.cpp:(.text+0x5a): first defined here term.o: In function Term::compareByWeight(Term, Term)': term.cpp:(.text+0xea): multiple definition ofTerm::compareByWeight(Term, Term)’ autocomplete.o:autocomplete.cpp:(.text+0xea): first defined here term.o: In function Term::compareByPrefix(Term, Term, int)': term.cpp:(.text+0x136): multiple definition ofTerm::compareByPrefix(Term, Term, int)’ autocomplete.o:autocomplete.cpp:(.text+0x136): first defined here term.o: In function operator<(Term, Term)': term.cpp:(.text+0x25a): multiple definition ofoperator<(Term, Term)’ autocomplete.o:autocomplete.cpp:(.text+0x25a): first defined here term.o: In function operator>(Term, Term)': term.cpp:(.text+0x28f): multiple definition ofoperator>(Term, Term)’ autocomplete.o:autocomplete.cpp:(.text+0x28f): first defined here term.o: In function operator<<(std::ostream&, Term const&)': term.cpp:(.text+0x2c4): multiple definition ofoperator<<(std::ostream&, Term const&)’
Is there anything that looks wrong with my header includes? Thanks
- Forum
- Beginners
- First defined here error
First defined here error
Im trying to learn some C++ but I get a error at line 8 saying first defined here. I cant find out whats wrong
|
|
Last edited on
That worked thanks, Any idea why it did that?
You broke something in previous project. It is impossible to say for sure.
I think, you had more than one file with main() fucntion in your project.
Oh ok thank you!
Topic archived. No new replies allowed.
make -f Makefile.Release
make[1]: Entering directory `/media/disk-1/Graphica3′
g++ -o Graphica3 release/main.o release/graphica3.o release/moc_graphica3.o -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib -lpng -lSM -lICE -pthread -pthread -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread
release/graphica3.o: In function `VolumePoint::VolumePoint()’:
graphica3.cpp:(.text+0x0): multiple definition of `VolumePoint::VolumePoint()’
release/main.o:main.cpp:(.text+0x0): first defined here
release/graphica3.o: In function `VolumePoint::VolumePoint()’:
graphica3.cpp:(.text+0x20): multiple definition of `VolumePoint::VolumePoint()’
release/main.o:main.cpp:(.text+0x20): first defined here
release/graphica3.o: In function `VolumePoint::VolumePoint(double, double, double)’:
graphica3.cpp:(.text+0x40): multiple definition of `VolumePoint::VolumePoint(double, double, double)’
release/main.o:main.cpp:(.text+0x40): first defined here
release/graphica3.o: In function `VolumePoint::VolumePoint(double, double, double)’:
graphica3.cpp:(.text+0x60): multiple definition of `VolumePoint::VolumePoint(double, double, double)’
release/main.o:main.cpp:(.text+0x60): first defined here
release/graphica3.o: In function `VolumePoint::VolumePoint(VolumePoint const&)’:
graphica3.cpp:(.text+0x80): multiple definition of `VolumePoint::VolumePoint(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x80): first defined here
release/graphica3.o: In function `VolumePoint::VolumePoint(VolumePoint const&)’:
graphica3.cpp:(.text+0xa0): multiple definition of `VolumePoint::VolumePoint(VolumePoint const&)’
release/main.o:main.cpp:(.text+0xa0): first defined here
release/graphica3.o: In function `VolumePoint::setX(double)’:
graphica3.cpp:(.text+0xc0): multiple definition of `VolumePoint::setX(double)’
release/main.o:main.cpp:(.text+0xc0): first defined here
release/graphica3.o: In function `VolumePoint::setY(double)’:
graphica3.cpp:(.text+0xd0): multiple definition of `VolumePoint::setY(double)’
release/main.o:main.cpp:(.text+0xd0): first defined here
release/graphica3.o: In function `VolumePoint::setZ(double)’:
graphica3.cpp:(.text+0xe0): multiple definition of `VolumePoint::setZ(double)’
release/main.o:main.cpp:(.text+0xe0): first defined here
release/graphica3.o: In function `VolumePoint::getX() const’:
graphica3.cpp:(.text+0xf0): multiple definition of `VolumePoint::getX() const’
release/main.o:main.cpp:(.text+0xf0): first defined here
release/graphica3.o: In function `VolumePoint::getY() const’:
graphica3.cpp:(.text+0x100): multiple definition of `VolumePoint::getY() const’
release/main.o:main.cpp:(.text+0x100): first defined here
release/graphica3.o: In function `VolumePoint::getZ() const’:
graphica3.cpp:(.text+0x110): multiple definition of `VolumePoint::getZ() const’
release/main.o:main.cpp:(.text+0x110): first defined here
release/graphica3.o: In function `VolumePoint::operator!=(VolumePoint const&)const’:
graphica3.cpp:(.text+0x120): multiple definition of `VolumePoint::operator!=(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x120): first defined here
release/graphica3.o: In function `VolumePoint::operator==(VolumePoint const&)const’:
graphica3.cpp:(.text+0x170): multiple definition of `VolumePoint::operator==(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x170): first defined here
release/graphica3.o: In function `VolumePoint::operator=(VolumePoint const&)’:
graphica3.cpp:(.text+0x1c0): multiple definition of `VolumePoint::operator=(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x1c0): first defined here
release/graphica3.o: In function `Perspective::Perspective(double)’:
graphica3.cpp:(.text+0x1e0): multiple definition of `Perspective::Perspective(double)’
release/main.o:main.cpp:(.text+0x1e0): first defined here
release/graphica3.o: In function `Perspective::Perspective(double)’:
graphica3.cpp:(.text+0x1f0): multiple definition of `Perspective::Perspective(double)’
release/main.o:main.cpp:(.text+0x1f0): first defined here
release/graphica3.o: In function `Perspective::setDist(double)’:
graphica3.cpp:(.text+0x200): multiple definition of `Perspective::setDist(double)’
release/main.o:main.cpp:(.text+0x200): first defined here
release/graphica3.o: In function `Side::anglesCount() const’:
graphica3.cpp:(.text+0x210): multiple definition of `Side::anglesCount() const’
release/main.o:main.cpp:(.text+0x210): first defined here
release/graphica3.o: In function `Side::nearer(Side const&) const’:
graphica3.cpp:(.text+0x220): multiple definition of `Side::nearer(Side const&) const’
release/main.o:main.cpp:(.text+0x220): first defined here
release/graphica3.o: In function `Side::visible() const’:
graphica3.cpp:(.text+0x270): multiple definition of `Side::visible() const’
release/main.o:main.cpp:(.text+0x270): first defined here
release/graphica3.o: In function `Perspective::makeFlat(VolumePoint const&)’:
graphica3.cpp:(.text+0x370): multiple definition of `Perspective::makeFlat(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x8d0): first defined here
release/graphica3.o: In function `Transform::transformate(VolumePoint const&)const’:
graphica3.cpp:(.text+0x3b0): multiple definition of `Transform::transformate(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x910): first defined here
release/graphica3.o: In function `Side::~Side()’:
graphica3.cpp:(.text+0x420): multiple definition of `Side::~Side()’
release/main.o:main.cpp:(.text+0x3a0): first defined here
release/graphica3.o: In function `Side::~Side()’:
graphica3.cpp:(.text+0x440): multiple definition of `Side::~Side()’
release/main.o:main.cpp:(.text+0x3c0): first defined here
release/graphica3.o: In function `Side::operator=(Side const&)’:
graphica3.cpp:(.text+0x460): multiple definition of `Side::operator=(Side const&)’
release/main.o:main.cpp:(.text+0x3e0): first defined here
release/graphica3.o: In function `Side::Side(Side const&)’:
graphica3.cpp:(.text+0x510): multiple definition of `Side::Side(Side const&)’
release/main.o:main.cpp:(.text+0x490): first defined here
release/graphica3.o: In function `Side::Side(Side const&)’:
graphica3.cpp:(.text+0x5c0): multiple definition of `Side::Side(Side const&)’
release/main.o:main.cpp:(.text+0x540): first defined here
release/graphica3.o: In function `Side::Side(VolumePoint const*, unsigned int)’:
graphica3.cpp:(.text+0x670): multiple definition of `Side::Side(VolumePoint const*, unsigned int)’
release/main.o:main.cpp:(.text+0x5f0): first defined here
release/graphica3.o: In function `Side::Side(VolumePoint const*, unsigned int)’:
graphica3.cpp:(.text+0x7e0): multiple definition of `Side::Side(VolumePoint const*, unsigned int)’
release/main.o:main.cpp:(.text+0x760): first defined here
release/graphica3.o: In function `Transform::modifyTransform(double, double, double, double, double, double, double)’:
graphica3.cpp:(.text+0x950): multiple definition of `Transform::modifyTransform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0x980): first defined here
release/graphica3.o: In function `Transform::setTransform(double, double, double, double, double, double, double)’:
graphica3.cpp:(.text+0xb10): multiple definition of `Transform::setTransform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0xb40): first defined here
release/graphica3.o: In function `Transform::Transform(double, double, double, double, double, double, double)’:
graphica3.cpp:(.text+0xd80): multiple definition of `Transform::Transform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0xdb0): first defined here
release/graphica3.o: In function `Transform::Transform(double, double, double, double, double, double, double)’:
graphica3.cpp:(.text+0xff0): multiple definition of `Transform::Transform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0x1020): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint()’:
moc_graphica3.cpp:(.text+0x0): multiple definition of `VolumePoint::VolumePoint()’
release/main.o:main.cpp:(.text+0x0): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint()’:
moc_graphica3.cpp:(.text+0x20): multiple definition of `VolumePoint::VolumePoint()’
release/main.o:main.cpp:(.text+0x20): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint(double, double, double)’:
moc_graphica3.cpp:(.text+0x40): multiple definition of `VolumePoint::VolumePoint(double, double, double)’
release/main.o:main.cpp:(.text+0x40): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint(double, double, double)’:
moc_graphica3.cpp:(.text+0x60): multiple definition of `VolumePoint::VolumePoint(double, double, double)’
release/main.o:main.cpp:(.text+0x60): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint(VolumePoint const&)’:
moc_graphica3.cpp:(.text+0x80): multiple definition of `VolumePoint::VolumePoint(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x80): first defined here
release/moc_graphica3.o: In function `VolumePoint::VolumePoint(VolumePoint const&)’:
moc_graphica3.cpp:(.text+0xa0): multiple definition of `VolumePoint::VolumePoint(VolumePoint const&)’
release/main.o:main.cpp:(.text+0xa0): first defined here
release/moc_graphica3.o: In function `VolumePoint::setX(double)’:
moc_graphica3.cpp:(.text+0xc0): multiple definition of `VolumePoint::setX(double)’
release/main.o:main.cpp:(.text+0xc0): first defined here
release/moc_graphica3.o: In function `VolumePoint::setY(double)’:
moc_graphica3.cpp:(.text+0xd0): multiple definition of `VolumePoint::setY(double)’
release/main.o:main.cpp:(.text+0xd0): first defined here
release/moc_graphica3.o: In function `VolumePoint::setZ(double)’:
moc_graphica3.cpp:(.text+0xe0): multiple definition of `VolumePoint::setZ(double)’
release/main.o:main.cpp:(.text+0xe0): first defined here
release/moc_graphica3.o: In function `VolumePoint::getX() const’:
moc_graphica3.cpp:(.text+0xf0): multiple definition of `VolumePoint::getX() const’
release/main.o:main.cpp:(.text+0xf0): first defined here
release/moc_graphica3.o: In function `VolumePoint::getY() const’:
moc_graphica3.cpp:(.text+0x100): multiple definition of `VolumePoint::getY() const’
release/main.o:main.cpp:(.text+0x100): first defined here
release/moc_graphica3.o: In function `VolumePoint::getZ() const’:
moc_graphica3.cpp:(.text+0x110): multiple definition of `VolumePoint::getZ() const’
release/main.o:main.cpp:(.text+0x110): first defined here
release/moc_graphica3.o: In function `VolumePoint::operator!=(VolumePoint const&) const’:
moc_graphica3.cpp:(.text+0x120): multiple definition of `VolumePoint::operator!=(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x120): first defined here
release/moc_graphica3.o: In function `VolumePoint::operator==(VolumePoint const&) const’:
moc_graphica3.cpp:(.text+0x170): multiple definition of `VolumePoint::operator==(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x170): first defined here
release/moc_graphica3.o: In function `VolumePoint::operator=(VolumePoint const&)’:
moc_graphica3.cpp:(.text+0x1c0): multiple definition of `VolumePoint::operator=(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x1c0): first defined here
release/moc_graphica3.o: In function `Perspective::Perspective(double)’:
moc_graphica3.cpp:(.text+0x1e0): multiple definition of `Perspective::Perspective(double)’
release/main.o:main.cpp:(.text+0x1e0): first defined here
release/moc_graphica3.o: In function `Perspective::Perspective(double)’:
moc_graphica3.cpp:(.text+0x1f0): multiple definition of `Perspective::Perspective(double)’
release/main.o:main.cpp:(.text+0x1f0): first defined here
release/moc_graphica3.o: In function `Perspective::setDist(double)’:
moc_graphica3.cpp:(.text+0x200): multiple definition of `Perspective::setDist(double)’
release/main.o:main.cpp:(.text+0x200): first defined here
release/moc_graphica3.o: In function `Side::anglesCount() const’:
moc_graphica3.cpp:(.text+0x210): multiple definition of `Side::anglesCount() const’
release/main.o:main.cpp:(.text+0x210): first defined here
release/moc_graphica3.o: In function `Side::nearer(Side const&) const’:
moc_graphica3.cpp:(.text+0x220): multiple definition of `Side::nearer(Side const&) const’
release/main.o:main.cpp:(.text+0x220): first defined here
release/moc_graphica3.o: In function `Side::visible() const’:
moc_graphica3.cpp:(.text+0x270): multiple definition of `Side::visible() const’
release/main.o:main.cpp:(.text+0x270): first defined here
release/moc_graphica3.o: In function `Side::~Side()’:
moc_graphica3.cpp:(.text+0x370): multiple definition of `Side::~Side()’
release/main.o:main.cpp:(.text+0x3a0): first defined here
release/moc_graphica3.o: In function `Side::~Side()’:
moc_graphica3.cpp:(.text+0x390): multiple definition of `Side::~Side()’
release/main.o:main.cpp:(.text+0x3c0): first defined here
release/moc_graphica3.o: In function `Side::operator=(Side const&)’:
moc_graphica3.cpp:(.text+0x3b0): multiple definition of `Side::operator=(Sideconst&)’
release/main.o:main.cpp:(.text+0x3e0): first defined here
release/moc_graphica3.o: In function `Side::Side(Side const&)’:
moc_graphica3.cpp:(.text+0x460): multiple definition of `Side::Side(Side const&)’
release/main.o:main.cpp:(.text+0x490): first defined here
release/moc_graphica3.o: In function `Side::Side(Side const&)’:
moc_graphica3.cpp:(.text+0x510): multiple definition of `Side::Side(Side const&)’
release/main.o:main.cpp:(.text+0x540): first defined here
release/moc_graphica3.o: In function `Side::Side(VolumePoint const*, unsignedint)’:
moc_graphica3.cpp:(.text+0x5c0): multiple definition of `Side::Side(VolumePoint const*, unsigned int)’
release/main.o:main.cpp:(.text+0x5f0): first defined here
release/moc_graphica3.o: In function `Side::Side(VolumePoint const*, unsignedint)’:
moc_graphica3.cpp:(.text+0x730): multiple definition of `Side::Side(VolumePoint const*, unsigned int)’
release/main.o:main.cpp:(.text+0x760): first defined here
release/moc_graphica3.o: In function `Perspective::makeFlat(VolumePoint const&)’:
moc_graphica3.cpp:(.text+0x8a0): multiple definition of `Perspective::makeFlat(VolumePoint const&)’
release/main.o:main.cpp:(.text+0x8d0): first defined here
release/moc_graphica3.o: In function `Transform::transformate(VolumePoint const&) const’:
moc_graphica3.cpp:(.text+0x8e0): multiple definition of `Transform::transformate(VolumePoint const&) const’
release/main.o:main.cpp:(.text+0x910): first defined here
release/moc_graphica3.o: In function `Transform::modifyTransform(double, double, double, double, double, double, double)’:
moc_graphica3.cpp:(.text+0x950): multiple definition of `Transform::modifyTransform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0x980): first defined here
release/moc_graphica3.o: In function `Transform::setTransform(double, double,double, double, double, double, double)’:
moc_graphica3.cpp:(.text+0xb10): multiple definition of `Transform::setTransform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0xb40): first defined here
release/moc_graphica3.o: In function `Transform::Transform(double, double, double, double, double, double, double)’:
moc_graphica3.cpp:(.text+0xd80): multiple definition of `Transform::Transform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0xdb0): first defined here
release/moc_graphica3.o: In function `Transform::Transform(double, double, double, double, double, double, double)’:
moc_graphica3.cpp:(.text+0xff0): multiple definition of `Transform::Transform(double, double, double, double, double, double, double)’
release/main.o:main.cpp:(.text+0x1020): first defined here
collect2: ld returned 1 exit status
make[1]: *** [Graphica3] Ошибка 1
make[1]: Leaving directory `/media/disk-1/Graphica3′
make: *** [release] Ошибка 2
The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
The content of commands.h after the pre-processor phase looks like this:
#ifndef COMMANDS_H_
#define COMMANDS_H_
// function definition
void f123(){
}
// function declaration
void f123();
#endif /* COMMANDS_H_ */
This is an error because you can’t declare a function after its definition in C. If you swapped #include "commands.c" and the function declaration the error shouldn’t happen because, now, the function prototype comes before the function declaration.
However, including a .c file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h in commands.c and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123(); // function declaration
#endif
commands.c
#include "commands.h"
void f123(){} // function definition
I had a similar issue when not using inline for my global function that was included in two places.
You should not include commands.c in your header file. In general, you should not include .c files. Rather, commands.c should include commands.h. As defined here, the C preprocessor is inserting the contents of commands.c into commands.h where the include is. You end up with two definitions of f123 in commands.h.
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123();
#endif
commands.c
#include "commands.h"
void f123()
{
/* code */
}
Tags:
C
Eclipse
Include
Definition
Multiple Definition Error
Related
Вопрос:
Я пытаюсь скомпилировать программу C, используя libxml2 в Eclipse. Похоже, что у моего кода нет проблем, но возникают ошибки при создании моего проекта.
Вывод ошибки приведен в этом снимке экрана: https://drive.google.com/file/d/0BwV-0_2diIaaQlZHM2Fwa2R0LWc/edit
Перед этой ошибкой у меня была ошибка “Неопределенная ссылка на”, но это было потому, что я забыл связать библиотеку libxml2. Теперь это проблема на скриншоте. Я не делаю.
[EDITED] Я решил свою проблему, мне просто нужно положить -nostartfiles в флаги компоновщика.
Лучший ответ:
Я решил свою проблему, мне просто нужно написать -nostartfiles в поле “Флаги компоновщика”: D Чтобы найти флажок “Флаги компоновщика”, перейдите в свой проект> “Свойства”> “C/С++”> “Настройки”> “Компилятор GCC C”> “Разное”
Это.
Спасибо за помощь.
Ответ №1
Я не думаю, что ваше решение приемлемо, я думаю, что это приведет к той же проблеме, когда ваш проект будет выполнен в другой среде (другая конфигурация затмения, запуск из консоли и т.д.). Эта ошибка возникает, когда вы определили одну и ту же функцию больше чем один раз в вашем проекте, и я готов поспорить, потому что вы определили ту же функцию, что и в библиотеке.
Например, если у меня есть lib1.h с функцией hello(), а затем напишите ту же функцию в main.c (связав связанную библиотеку), что проблема возникнет. Фактически, на вашем изображении я вижу “множественное определение __data_start”,
Поэтому я думаю, что вам просто нужно изменить имя проблемной функции, и оно будет исправлено.
Кроме того, эта проблема будет вызвана, если вы включите одну и ту же библиотеку более одного раза в свою программу, но ее можно решить с помощью препроцессора (если вас это интересует, google it, так как это будет не по теме и сделать мой ответ слишком долго)
The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
The content of commands.h after the pre-processor phase looks like this:
#ifndef COMMANDS_H_
#define COMMANDS_H_
// function definition
void f123(){
}
// function declaration
void f123();
#endif /* COMMANDS_H_ */
This is an error because you can’t declare a function after its definition in C. If you swapped #include "commands.c" and the function declaration the error shouldn’t happen because, now, the function prototype comes before the function declaration.
However, including a .c file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h in commands.c and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123(); // function declaration
#endif
commands.c
#include "commands.h"
void f123(){} // function definition
~ Answered on 2015-06-13 17:59:18
05-18-2018
#1
![]()
Registered User
Multiple definition of ‘ ‘ first defined here
So I implemented a linked list and a separate chaining hashtable of a struct called Objective, so that I can implement some functions to work with that struct.
Hashtable.c:
Code:
#include <stdlib.h>#include <string.h> #include <stdio.h> #include "HASHTABLE.h" #define hash(A,B) (A%B) static link *heads; static int M; void Init(int m){ int i; M = m; heads = (link*)malloc(M*sizeof(link)); for(i = 0; i < M; i++) heads[i] = NULL; } pObjective search(unsigned long id){ int i = hash(id, M); return searchList(heads[i], id); } void insert(pObjective o){ int i = hash(o->id, M); heads[i] = insertBegin(heads[i], o); } void delete(unsigned long id){ int i = hash(id, M); heads[i] = removeList(heads[i], id); } link insertBegin(link h, pObjective obj){ link new = (link)malloc(sizeof(struct nodehash)); new->obj = obj; new->next = h; return new; } pObjective searchList(link h, unsigned long id){ link t, x; for(t = h; t != NULL; t = t->next){ if(t->obj->id == id) x = t; } return x->obj; } link removeList(link h, unsigned long id){ link t, x, z; for(t = h; t != NULL; t = t->next){ if(t->next->obj->id == id) x = t; } z = x->next; x->next = z->next; free(z); return h; }Hashtable.h:
Code:
#ifndef HASHTABLE_H#define HASHTABLE_H #include <string.h> #include <stdio.h> #include <stdlib.h> typedef struct Objective{ char name [8000]; unsigned long id, duration, deps [9000]; int hasDeps; }*pObjective; typedef struct nodehash{ pObjective obj; struct nodehash*next; }*link; void Init(int M); pObjective search(unsigned long id); void insert(pObjective o); void delete(unsigned long id); link insertBegin(link h, pObjective obj); pObjective searchList(link h, unsigned long id); link removeList(link h, unsigned long id); #endifList.c:
Code:
#include <stdlib.h>#include <string.h> #include <stdio.h> #include "LISTA.h" links insertEnd(links head, lObjective obj){ links t; links new = (links)malloc(sizeof(struct node)); new->obj = obj; new->next = NULL; if(head == NULL) return new; for(t = head; t->next != NULL; t = t->next) ; t->next = new; return head; } int checkId(links head, unsigned long id){ links t; int count = 0; for(t = head; t != NULL; t = t->next){ if(t->obj->id == id) count++; } return count; } links removeList2(links head, unsigned long id){ links t, x, z; for(t = head; t != NULL; t = t->next){ if(t->next->obj->id == id) x = t; } z = x->next; x->next = z->next; free(z); return head; } void print(links head){ links t; int i; for(t = head; t!=NULL; t = t->next){ printf("%lu %s %lu ", t->obj->id, t->obj->name, t->obj->duration); for(i = 0; i < 9000; i++){ printf("%lu ",t->obj->deps[i]); } printf("n"); } }List.h:
Code:
#ifndef LISTA_H#define LISTA_H #include <stdio.h> #include <stdlib.h> typedef struct Objectives{ char name [8000]; unsigned long id, duration, deps [9000]; int hasDeps; }*lObjective; typedef struct node{ lObjective obj; struct node *next; }*links; links head = NULL; links insertEnd(links head, lObjective obj); int checkId(links head, unsigned long id); links removeList2(links head, unsigned long id); void print(links head); #endifObjective.c:
Code:
#include <stdlib.h>#include <string.h> #include <stdio.h> #include "OBJECTIVES.h" int existsDep(unsigned long dep [9000]){ int count = 0, i; for(i=0; i < 9000; i++){ if(checkId(head, dep[i]) == 0) count++; } if(count == 0) return 1; else return 0; } void newObjective(unsigned long id, char name [8000], unsigned long duration, unsigned long dep [9000]){ int i; pObjective obj = malloc(sizeof(pObjective)); lObjective ob = malloc(sizeof(lObjective)); obj->id = id; ob->id = id; obj->duration = duration; ob->duration = duration; obj->hasDeps = 1; ob->hasDeps= 1; strcpy(name, obj->name); strcpy(name, ob->name); for(i = 0; i < 9000; i++){ obj->deps[i] = dep[i]; ob->deps[i] = dep[i]; } if(checkId(head, id) != 0) printf("id already existsn"); else if(existsDep(dep) == 0){ printf("no such taskn"); } else{ insert(obj); insertEnd(head, ob); } free(obj); free(ob); } void newObjectiveNoDeps(unsigned long id, char name [8000], unsigned long duration){ pObjective obj = malloc(sizeof(pObjective)); lObjective ob = malloc(sizeof(lObjective)); obj->id = id; ob->id = id; obj->duration = duration; ob->duration = duration; obj->hasDeps = 0; ob->hasDeps = 0; strcpy(name, obj->name); strcpy(name, ob->name); if(checkId(head, id) != 0) printf("id already existsn"); else{ insert(obj); insertEnd(head, ob); } free(obj); free(ob); } void removeObj(unsigned long id){ pObjective obj = search(id); if(obj->hasDeps == 1) printf("task with depedenciesn"); else if(checkId(head, obj->id) == 0) printf("no such taskn"); else delete(id); removeList2(head, id); } void depend(unsigned long id){ pObjective obj = search(id); int i; if(obj->hasDeps == 0) printf("%lu: no depedenciesn", id); else if(checkId(head, obj->id) == 0) printf("no such taskn"); else{ printf("%lu: ",id ); for(i = 0; i < 9000; i++){ printf("%lu ", obj->deps[i]); } } printf("n"); } void duration(){ print(head); }Objective.h:
Code:
#ifndef OBJECTIVES_H#define OBJECTIVES_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include "LISTA.h" #include "HASHTABLE.h" int existsDep(unsigned long dep [9000]); void newObjective(unsigned long id, char name [8000], unsigned long duration, unsigned long dep [9000]); void newObjectiveNoDeps(unsigned long id, char name [8000], unsigned long duration); void removeObj(unsigned long id); void depend(unsigned long id); void duration(); #endifMy problem is in the head of the list, i keep getting this error:
Code:
/tmp/ccIa32rv.o:(.bss+0x0): multiple definition of `head'/tmp/ccAvumDO.o:(.bss+0x0): first defined here /tmp/ccuVImET.o:(.bss+0x0): multiple definition of `head' /tmp/ccAvumDO.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit statusAnd i don’t know where I’m wrong… can anyone help me?