Hi! Everybody,

I have several c++ sources files as following:

main.cpp:

#include "A.hpp"

int main()
{
return 0;
}

A.hpp:

#ifndef A_HPP_
#define A_HPP_

#include "B/B.hpp"

#endif /* A_HPP_ */

B/B.hpp:

#ifndef B_HPP_
#define B_HPP_

class Foo
{
int foo(int val);
};

#endif /* B_HPP_ */

B/B.cpp:

#include "B.hpp"

int Foo::foo(int val) { return val;}

when I compiled them using (actually the commands were generated by Eclipse IDE):
g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oB\B.o ..\B\B.cpp
g++ -otest.exe main.o B\B.o B.o

I got these error messages:
B.o: In function `ZN3Foo3fooEi':
C:/Users/sam/workspace/test/Debug/../B.cpp:10: multiple definition of `Foo::foo(int)'
B\B.o:C:/Users/sam/workspace/test/Debug/../B/B.cpp:10: first defined here
collect2: ld returned 1 exit status

thx

Our Software Development forum category encompasses topics related to application programming and software design. When posting programming code, encase it in (code), (code=syntax), where 'syntax' is any language found within our Code Snippets section, or (icode), for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.
Our C++ forum is the place for Q&A-style discussions related to this popular language. Note we have a separate C forum for non-OOP straight C. Please direct C++ questions directly related to game development to our Game Development forum.
C++ Forum Threads
RSS
Forum Tools

Views: 48,777
8th Jun 2004
Announcement
Announcement: Please use BB Code and Inlinecode tags

Now, why did you ignore all the rules on how to post?

g++ -otest.exe main.o B\B.o B.o

Something smells funny about linking with B.o twice.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Now, why did you ignore all the rules on how to post?

really sorry about ignoring the rules. Can I edit my post? I cannot find any link to edit my post ...

really sorry about ignoring the rules. Can I edit my post? I cannot find any link to edit my post ...

You can edit your post for about half an hour after you post it, then you can't after that. You should just add a new post now instead.

This article has been dead for over six months. Start a new discussion instead.