Using (G++-5) + (Xcode) + (Makefile) + (Autocomplete)
Hey guys, if you have been trying to use Xcode for your C++ projects with “Makefile” and “g++-5”, you are in the right place. I am about to tell you how you can achieve this :)
First of all, you need to tell Xcode, how to find g++-5. As you may know, after Xcode6 we cannot choose g++ as our compiler in Xcode. (I assume you already have gcc-5) (This part is mostly for autocompletion with g++-5, it is nothing to do with compiling with Makefile. Only required step is installing g++-5)
To be able to see g++-5 on the compiler section, download this extension and put related plugin(s) to the Xcode/Plug-ins directory.
Since I only want to use “gcc-5.2”, I will copy “GCC 5.2.xcspec”.
cp GCC\ 5.2.xcspec /Applications/Xcode.app/Contents/Plugins/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/
Now, you can see “gcc-5” under “Build Settings -> Build Options”. For more information check this blog post.
After this step, lets continue with required steps. Lets create a “External Build System”.
Then, add new “Empty File” and “main.cpp” files to your project. (Before you start write your Makefile, be sure to set “Prefer Indent Using” to tabs in your Xcode)
I wrote a Makefile and yours also should be look like mine.
Now, when you try to run your project, you will see “Build Succeeded” message. But, it won’t run yet.
Go to “Product -> Scheme -> Edit Scheme”
Choose “Run Executable” to “Other” and find your executable file (When you build it, you created one).
Now, if you try to run your code from Xcode, you will see it runs.
Are we done? Not yet unless you want to use without autocomplete.
For autocompletion, “Add New Target” to your project as “Command Line Tool”.
This part created another “main.cpp” file we do not need. So, we should get rid of it. Choose the new “Command Line Tool” as our target for now and delete its “Compile Sources” items.
Also, delete new folder which contains newly (automatically) created “main.cpp”.
In the “Build Phases” tab of the “Command Line Tool Target” change compiler to “GCC-5.2”.
Choose “Project File” as target.
Change also compiler to “GCC-5.2” and delete the line “CLANG_CXX_LIBRARY”.
Choose the main file (and all other files you want to have autocomplete) we created and set its target to newly created “Command Line Tool” target.
Build your “Command Line Project” (In my case it is MakefileProject-doc) and go and run your actual project :) Thats all.
P.S. If you add another file, don’t forget to set their target to “Command Line Project”.
P.S2. Each time you add something, don’t forget to build your “Command Line Project”
Happy Coding :)