Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

obooyo

macrumors newbie
Original poster
Jul 22, 2008
8
0
I keep getting this duplicate error, and I am pretty sure nothing is duplicated here. I have a main.cpp and print.cpp in my source directory. The main.cpp looks like this:

#include <iostream>
#include "print.cpp"

using namespace std;


int main () {
// insert code here...
print();
return 0;
}

and the print.cpp is:


#include <iostream>

using namespace std;

void print()
{
cout << "no duplicate" <<endl;
}



It gives me an error of duplicate symbol print(). On the other hand, if i just get rid of the whole print.cpp, by making it blank (with /* and */), then it actually works. What is going on here??
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
This is, I'd say, a non-traditional use of #include. Generally you'd include a header file, not the implementation. Nonetheless, I tried the same thing and this worked without issue. You might want to post more code, and/or the command you used to compile and the exact output.

Please use code tags as well (perhaps even for the command and errors) so it is easier to read.

-Lee
 

obooyo

macrumors newbie
Original poster
Jul 22, 2008
8
0
This is in fact all the code I have. There's the "main.cpp", "print.h", "print.cpp". I left "print.h" blank. All I did was click on build and go, and I got this message in the Build Result window:

Building target “Testing” of project “Testing” with configuration “Debug” — (1 error)
Linking /Users/aaronchen/Documents/FX/Testing/build/Debug/Testing (1 error)
duplicate symbol print() in /Users/aaronchen/Documents/FX/Testing/build/Testing.build/Debug/Testing.build/Objects-normal/i386/print.o and /Users/aaronchen/Documents/FX/Testing/build/Testing.build/Debug/Testing.build/Objects-normal/i386/main.o
Command/Develper/usr/bin/g++-4.0 failed with exit code 1
Build Failed (1 error)

By the way, what is a code tag??

If this is not complete enough, I can try to take a screen shot.

Thanks
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I just built the object file for main from the command line, but it looks like XCode did a bit more.

Change print.h to have:
Code:
void print();

in it, then change:
Code:
#include "print.cpp"
in your main.cpp file to:
Code:
#include "print.h"

This will give a prototype for print() to the compiler when it's building main.o. The way I had compiled it the actual source of print.cpp was brought into my main file, but since I was not building an object file for print.cpp I did not get the error you did and it worked fine. The right way to do this is what I described above, including an implementation file is very rarely the right thing to do.

Code tags retain formatting of code and display it in a block in your post. There is a # sign in the formatting toolbar above your post. If you click that, it will give you the tags. I can't write them literally here without a lot of effort, but it is the word CODE in square brackets, and it closes with /CODE in square brackets. This makes it much easier to read and prevents substitution of smilies, etc. in code.

-Lee
 

AussieSusan

macrumors member
May 29, 2006
54
0
Melbourne, Australia
What I think is happening is that you are actually compiling the 'print()' function twice.

XCode will, be default, assume that you will want to compile each .c file separately and then combine all of the resulting object files together into a single executable.

When you compile the main program, you are including the implementation of the 'print()' function, because the 'include' pre-processor command simply performs a textual insertion of the included file into your code at that point.

Next, XCode will compile the 'print' code file to create another object file. You now have two object files, both of which contain the 'print()' function code.

When the linker tries to combine these two files, it finds that the 'print()' function is in both and so complains about the duplicate symbol.

There are rules and conventions about how the #include (and the #import) directives are supposed to be used, and why we separate declarations from implementations for a very good reason. I suspect that you may be going against these rules and conventions out of a lack of knowledge and understanding, in which case I would suggest that you go back to some basic books on C programming and understand what is going on here - you will same yourself a lot of grief in the long run and get to your goal much quicker.

Susan
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.