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

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
I'm new to XCode (v4.6.2) and I've been following the Stanford CS106 courses on iTunesU. I'm just starting CS106B and discovered that it's not really compatible with the newer version of XCode.

Now I don't mind messing around a bit to get things working and I found a "Blank Project" xcodeproj which has the Stanford libraries included and I can get some things working. But... if I copy the "Blank Project" folder, I can rename the copy of the project in XCode but if I try to replace the "main.cpp" file with another file such as some starter code supplied as part of the course, then building the project fails with an error that seems to say the linker can't find "_main" - even though there is definitely a "main" method in the new ".cpp" file I've added.

I'm assuming there is some setting in the project somewhere that defines the location of "main", but I've looked everywhere and can't find such a setting. I even wondered if the location of "main" was hard-coded to be "main.cpp" but I've got another project from the Stanford course website that only includes a file called "Warmup.cpp" in which "main" is defined and this works fine. But even comparing the settings I can find no difference. Any suggestions would be welcome...

Regards,

Suds
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Is main.cpp part of your target?

Simple test: If you add

#error

as the first line of your main.cpp, does the compiler show an error? If not, then you didn't make it part of your project.
 

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
Hi gnasher729 - thanks for the quick reply...

> Is main.cpp part of your target?

main.cpp is the file I removed, and replaced with add2.cpp - so I'm not sure, I am very new to this :)

However if I add "#error" to the add2.cpp file as you suggested, then XCode does indeed flag it as an error.

I'm wondering if an image might help...

http://suddick.net/images/xcodeproj.png

Funny thing is, if I add main.cpp back in - as well as add2.cpp - then the linker reports a duplicate symbol of main...


Suds
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
I've never run into this issue before, but I'm pretty sure it's a rule in C and pretty much every language that derives from C that you must have exactly one function named main(). That one function is how every C program starts - if you don't have one (or you have more than one) it won't know which lines of code to execute first.

Were I you, I'd probably copy the code they provided out of their main function and replace the Xcode template's main function with it.

Also, I suggest pressing the "quote" button next to the post you're responding to. The forums will send a notification to the person you're quoting, which helps to ensure that they'll see your response and it won't get buried in the forums.
 

objc

macrumors regular
Mar 14, 2007
160
26
replace the "main.cpp" file with another file such as some starter code supplied as part of the course, then building the project fails with an error that seems to say the linker can't find "_main" - even though there is definitely a "main" method in the new ".cpp" file I've added.

I'm assuming there is some setting in the project somewhere that defines the location of "main", but I've looked everywhere and can't find such a setting. I even wondered if the location of "main" was hard-coded to be "main.cpp" but I've got another project from the Stanford course website that only includes a file called "Warmup.cpp" in which "main" is defined and this works fine.

The Warmup.cpp project is irrelevant. The problem is likely that the file containing your main() is not included in the list of files given to the linker. After you attempt your build, there should be a file in your project folder/directory called projectname.LinkFileList, where projectname is the name of your project. That file contains a list of object files (ending in .o) for the linker. Those object files will be included when making the executable file.

I would suggest checking this file, and consider renaming your c++ source file to be main.cpp and see what happens.
 

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
I've never run into this issue before, but I'm pretty sure it's a rule in C and pretty much every language that derives from C that you must have exactly one function named main(). That one function is how every C program starts - if you don't have one (or you have more than one) it won't know which lines of code to execute first.

I think you are quite correct; when I have both main.cpp and add2.cpp included in the project it fails to build with a duplicate symbol error - there are 2 main's, but when I have only add2.cpp it seems to fail to find the main that is in there.

Were I you, I'd probably copy the code they provided out of their main function and replace the Xcode template's main function with it.

It's not actually an Xcode template I was using that had main.cpp in, it's a "blank project" from the Stanford web site but I had already tried what you suggested, just to check that the code worked and it did, but I wanted to know what I was doing wrong when trying to build the project with an alternative .cpp file.

Also, I suggest pressing the "quote" button next to the post you're responding to. The forums will send a notification to the person you're quoting, which helps to ensure that they'll see your response and it won't get buried in the forums.

Thank you for the suggestions, I hope I've managed to get the quoting right :)

Suds
 

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
Thank you objc:

The Warmup.cpp project is irrelevant.

I agree it's not relevant to why I can't build the new project but it did serve to demonstrate that I don't need to have my main() function in a file called main.cpp.


The problem is likely that the file containing your main() is not included in the list of files given to the linker. After you attempt your build, there should be a file in your project folder/directory called projectname.LinkFileList, where projectname is the name of your project. That file contains a list of object files (ending in .o) for the linker. Those object files will be included when making the executable file.

A good point. I checked the build folder and with my project as per the image I uploaded, so only add2.cpp, there is a file called add2.o in the build directory and this file is listed in the LinkFileList file you mentioned.

I would suggest checking this file, and consider renaming your c++ source file to be main.cpp and see what happens.

Hmm, I tried renaming the add2.cpp file to main.cpp (in XCode) and the build still failed with the unable to find main error. This confuses me even more as I would have expected that to work!


Suds
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
I wanted to know what I was doing wrong when trying to build the project with an alternative .cpp file.

Thank you for the suggestions, I hope I've managed to get the quoting right :)

You have quoted me properly.

In the organizer (left plane) click on the project icon at the top of the list (blue document icon) to the right of that it'll list your targets (there should only be one). Select your target. I don't have Xcode in front of me so I'm going from memory on this right now - there's a tab called "Build Phases" if I recall correctly, and under that tab it lists "Compile Sources". Make sure the source file where your main() function is declared is in that list - if it isn't, drag and drop it there.

I think that'll fix your issue.
 

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
Some further information:

After renaming my add2.cpp file to main.cpp as objc suggested and finding it didn't work, I decided to go back to the way things had been previously so renamed main.cpp back to add2.cpp (and changed the temporarily renamed main.bak back to main.cpp). Now, even if I have just main.cpp in the project (which used to work) I still get the missing _main error - what on earth is going on?!?

Suds

----------

In the organizer (left plane) click on the project icon at the top of the list (blue document icon) to the right of that it'll list your targets (there should only be one). Select your target. I don't have Xcode in front of me so I'm going from memory on this right now - there's a tab called "Build Phases" if I recall correctly, and under that tab it lists "Compile Sources". Make sure the source file where your main() function is declared is in that list - if it isn't, drag and drop it there.

I think that'll fix your issue.

Thanks again, that sounded promising, and although main.cpp was listed in that section, I deleted it and dragged and dropped the existing main.cpp into it. Unfortunately the build still failed - same error - missing _main.


Suds
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
Some further information:

After renaming my add2.cpp file to main.cpp as objc suggested and finding it didn't work, I decided to go back to the way things had been previously so renamed main.cpp back to add2.cpp (and changed the temporarily renamed main.bak back to main.cpp). Now, even if I have just main.cpp in the project (which used to work) I still get the missing _main error - what on earth is going on?!?

Suds

----------



Thanks again, that sounded promising, and although main.cpp was listed in that section, I deleted it and dragged and dropped the existing main.cpp into it. Unfortunately the build still failed - same error - missing _main.


Suds

And the .cpp file you have includes a function named main()? (The function is what it's looking for - the name of the file shouldn't matter so long as it's in that list.)
 

truehybridx

macrumors member
Dec 6, 2010
86
0
Make sure the .cpp is in the Compile SOurces list under Build Phases.

IDK why but xcode doesnt like adding source code to the actual target when you drag and drop or otherwise copy it
 

Suds

macrumors newbie
Original poster
May 27, 2013
7
0
NE England
And the .cpp file you have includes a function named main()? (The function is what it's looking for - the name of the file shouldn't matter so long as it's in that list.)

Yes, it does indeed have a main function.

I think I may have found, at least part of, the problem - there is a header file included with the *old* Stanford libraries called genlib.h which seems to redefine "main" as "Main", without this header I seem to be able to switch .cpp files without any issues (although why the first project I modified is now totally screwed up I have no idea!)

The code using the *new* libraries in the projects I've downloaded doesn't seem to need this genlib.h, so I seem to have a workable way of adding and compiling code. I'm going to try some other programs now and see how far I get...

Many thanks again :)

----------

Make sure the .cpp is in the Compile SOurces list under Build Phases.

IDK why but xcode doesnt like adding source code to the actual target when you drag and drop or otherwise copy it

Thanks truehybridx - I checked as you suggested, but as you might see in my previous post I think I have found part of the problem, but at least I now have a better idea where to check for problems like this - thanks again.

Suds
 

GuGog

macrumors newbie
Oct 18, 2014
2
0
Yes, it does indeed have a main function.

I think I may have found, at least part of, the problem - there is a header file included with the *old* Stanford libraries called genlib.h which seems to redefine "main" as "Main", without this header I seem to be able to switch .cpp files without any issues (although why the first project I modified is now totally screwed up I have no idea!)

The code using the *new* libraries in the projects I've downloaded doesn't seem to need this genlib.h, so I seem to have a workable way of adding and compiling code. I'm going to try some other programs now and see how far I get...

Many thanks again :)

----------





Thanks truehybridx - I checked as you suggested, but as you might see in my previous post I think I have found part of the problem, but at least I now have a better idea where to check for problems like this - thanks again.

Suds


I seem to be having the exact same problem... Although my main() is in main.cpp and I have another file test.cpp which has about 6 functions (does NOT have a main). I get a duplicate symbol error for each of my functions in test.cpp

My set up is:
One project
2 files - main.cpp, test.cpp
main.cpp - only contains main() , includes "test.cpp"
test.cpp - contains 6 functions (no MAIN).

I am clearly not getting something please help!

Error snippet:
duplicate symbol __Z15doSelectionSortv in:
/Users/gunjangogia/Library/Developer/Xcode/DerivedData/Preps-clulzdledtoniofdtifviugmqtwv/Build/Intermediates/Preps.build/Debug/Preps.build/Objects-normal/x86_64/main.o
/Users/gunjangogia/Library/Developer/Xcode/DerivedData/Preps-clulzdledtoniofdtifviugmqtwv/Build/Intermediates/Preps.build/Debug/Preps.build/Obje
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
I seem to be having the exact same problem... Although my main() is in main.cpp and I have another file test.cpp which has about 6 functions (does NOT have a main). I get a duplicate symbol error for each of my functions in test.cpp

My set up is:
One project
2 files - main.cpp, test.cpp
main.cpp - only contains main() , includes "test.cpp"
test.cpp - contains 6 functions (no MAIN).

I am clearly not getting something please help!

Error snippet:
duplicate symbol __Z15doSelectionSortv in:
/Users/gunjangogia/Library/Developer/Xcode/DerivedData/Preps-clulzdledtoniofdtifviugmqtwv/Build/Intermediates/Preps.build/Debug/Preps.build/Objects-normal/x86_64/main.o
/Users/gunjangogia/Library/Developer/Xcode/DerivedData/Preps-clulzdledtoniofdtifviugmqtwv/Build/Intermediates/Preps.build/Debug/Preps.build/Obje

The error is shown in red. Here's the solution: DO NOT include "test.cpp" from main.cpp. Compile the two files separately.

As you've done things, there are two definitions of every function except main(). Once in the compiled main.o (contains result of compiling main() and test.cpp), and again in the compiled test.o (contains result of compiling test.cpp).

If that doesn't make sense, then pretend to be the compiler. You're told to compile "main.cpp". In "main.cpp" there is a directive to include "test.cpp". So you insert the entire contents of "test.cpp" at that point in the file and proceed to compile the result. List all the functions that will be compiled in the output file main.o. The logical answer is every function directly defined in "main.cpp" PLUS every function defined in "test.cpp".

Next, you're told to compile "test.cpp", so you do. List all the functions in test.o. The logical answer is every function defined in test.cpp.

Now link main.o with test.o. You now have duplicate function definitions for every function EXCEPT main(). The reasons for why this is so should be apparent.
 

GuGog

macrumors newbie
Oct 18, 2014
2
0
The error is shown in red. Here's the solution: DO NOT include "test.cpp" from main.cpp. Compile the two files separately.

As you've done things, there are two definitions of every function except main(). Once in the compiled main.o (contains result of compiling main() and test.cpp), and again in the compiled test.o (contains result of compiling test.cpp).

If that doesn't make sense, then pretend to be the compiler. You're told to compile "main.cpp". In "main.cpp" there is a directive to include "test.cpp". So you insert the entire contents of "test.cpp" at that point in the file and proceed to compile the result. List all the functions that will be compiled in the output file main.o. The logical answer is every function directly defined in "main.cpp" PLUS every function defined in "test.cpp".

Next, you're told to compile "test.cpp", so you do. List all the functions in test.o. The logical answer is every function defined in test.cpp.

Now link main.o with test.o. You now have duplicate function definitions for every function EXCEPT main(). The reasons for why this is so should be apparent.


Thanks that makes sense. And what i've done is removed test.cpp from the list of files that needs to be compiled...

Please help me understand the paradigm here. When we have multiple files in a project and one of them is main.cpp wouldn't main invariably "include" some of the other files if not all as main is the one which gets stuff going... I am not sure how this should be normally done. Should I be having a different "folder" with all the files i want main to include instead of removing those files from the compile sources of the project?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
Thanks that makes sense. And what i've done is removed test.cpp from the list of files that needs to be compiled...
If you're using Xcode, and I assume you are since that's what this thread is about, then that's the wrong approach.

What you should do is take out the #include of test.cpp, or any other includes that refer to .c or .cpp files. Includes that refer to .h files are ok.

Please help me understand the paradigm here. When we have multiple files in a project and one of them is main.cpp wouldn't main invariably "include" some of the other files if not all as main is the one which gets stuff going... I am not sure how this should be normally done. Should I be having a different "folder" with all the files i want main to include instead of removing those files from the compile sources of the project?
What are you learning from? Following a tutorial? Reading a book? Taking a class? Watching a video? Something else?

Whatever it is, it should have explained the exact procedure for compiling things. If you're learning on your own without reference to known-working instructions, you're going to make lots of mistakes. It's much better to follow working instructions.
 

AntoineLec

macrumors member
Jul 31, 2012
30
2
You need to read about two concepts: separate compilation, and linker.

Trying to include a cpp file inside another cpp file is a sign that you don't understand the process of compilation. You should run a terminal and play with make and Makefiles for some time. Then come back to your IDE.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.