PDA

View Full Version : XCode new user - using C++




Suds
May 27, 2013, 09:45 AM
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
May 27, 2013, 10:01 AM
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
May 27, 2013, 10:23 AM
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
May 27, 2013, 12:26 PM
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
May 27, 2013, 04:07 PM
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
May 27, 2013, 05:30 PM
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
May 27, 2013, 05:42 PM
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
May 27, 2013, 06:28 PM
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
May 27, 2013, 06:39 PM
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
May 27, 2013, 07:13 PM
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
May 27, 2013, 08:37 PM
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
May 28, 2013, 04:17 AM
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