I am moving away from simply using a Makefile and text editor. I would like to convert my Makefile so that I can use XCode properly. How can I convert this Makefile?
all:
gcc engine.c $(MRUBY_HOME)/build/host/lib/libmruby.a -I $(MRUBY_HOME)/include $(shell pkg-config --cflags --libs sdl2)
there is nothing stopping you from using a makefile in Xcode. just setup your target as an external build system and populate the "Build Tool" setting with the path to make (/usr/bin/make) and the "Directory" field pointing to the directory in your project containing the makefile. you can populate the "Arguments" field with any arguments needed for your makefile; these can be done with Xcode variables such as $(ACTION) (build, clean) and $(CONFIGURATION) (debug, release).
If you want to do away with the makefile completely then you will need to first add the source files and libraries to your target under the "build phases" tab for your build target. Any shell scripts can be added as a new "run script" build phase. other settings can be set from the "build settings" tab; specifically the "other c flags" field, although most compiler and linker settings should appear as options in that section.
Related
I have a C library which I need to use in my ongoing Xcode project. I used Cmake to build and install that Library. I gave the paths to Headers files and I can include the files into my Xcode project. However, when I run the project it says:
linker command failed with exit code 1 (use -v to see invocation)
I suspect that problem is with Linker Flags. I haven't included any Linker Flags and I don't know which flags do I need to add. Please tell me where in the library would I get all the required Linker Flags.
git hub link of the library which I want to add:
https://github.com/cbalint13/pba
From xcode left side window.
Under Project Navigator, click on your project, click on build settings.
Select "All" and "Combined" sections, then search for "Linker Flag"
Don't forget to switch to your target's build settings as well. As you might change just the "Project", not the "Target".
Is there a way to configure CLion to use a local makefile to compile code, rather than CMake? I can't seem to find the way to do it from the build options.
Update: If you are using CLion 2020.2, then it already supports Makefiles. If you are using an older version, read on.
Even though currently only CMake is supported, you can instruct CMake to call make with your custom Makefile. Edit your CMakeLists.txt adding one of these two commands:
add_custom_target
add_custom_command
When you tell CLion to run your program, it will try to find an executable with the same name of the target in the directory pointed by PROJECT_BINARY_DIR. So as long as your make generates the file where CLion expects, there will be no problem.
Here is a working example:
Tell CLion to pass its $(PROJECT_BINARY_DIR) to make
This is the sample CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.4)
project(mytest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(mytest COMMAND make -C ${mytest_SOURCE_DIR}
CLION_EXE_DIR=${PROJECT_BINARY_DIR})
Tell make to generate the executable in CLion's directory
This is the sample Makefile:
all:
echo Compiling $(CLION_EXE_DIR)/$# ...
g++ mytest.cpp -o $(CLION_EXE_DIR)/mytest
That is all, you may also want to change your program's working directory so it executes as it is when you run make from inside your directory. For this edit: Run -> Edit Configurations ... -> mytest -> Working directory
While this is one of the most voted feature requests, there is one plugin available, by Victor Kropp, that adds support to makefiles:
Makefile support plugin for IntelliJ IDEA
Install
You can install directly from the official repository:
Settings > Plugins > search for makefile > Search in repositories > Install > Restart
Use
There are at least three different ways to run:
Right click on a makefile and select Run
Have the makefile open in the editor, put the cursor over one target (anywhere on the line), hit alt + enter, then select make target
Hit ctrl/cmd + shift + F10 on a target (although this one didn't work for me on a mac).
It opens a pane named Run target with the output.
Newest version has better support literally for any generated Makefiles, through the compiledb
Three steps:
install compiledb
pip install compiledb
run a dry make
compiledb -n make
(do the autogen, configure if needed)
there will be a compile_commands.json file generated
open the project and you will see CLion will load info from the json file.
If you your CLion still try to find CMakeLists.txt and cannot read compile_commands.json, try to remove the entire folder, re-download the source files, and redo step 1,2,3
Orignal post: Working with Makefiles in CLion using Compilation DB
To totally avoid using CMAKE, you can simply:
Build your project as you normally with Make through the terminal.
Change your CLion configurations, go to (in top bar) :
Run -> Edit Configurations -> yourProjectFolder
Change the Executable to the one generated with Make
Change the Working directory to the folder holding your executable (if needed)
Remove the Build task in the Before launch:Activate tool window box
And you're all set! You can now use the debug button after your manual build.
Currently, only CMake is supported by CLion. Others build systems will be added in the future, but currently, you can only use CMake.
An importer tool has been implemented to help you to use CMake.
Edit:
Source : http://blog.jetbrains.com/clion/2014/09/clion-answers-frequently-asked-questions/
I am not very familiar with CMake and could not use Mondkin's solution directly.
Here is what I came up with in my CMakeLists.txt using the latest version of CLion (1.2.4) and MinGW on Windows (I guess you will just need to replace all:
g++ mytest.cpp -o bin/mytest by make if you are not using the same setup):
cmake_minimum_required(VERSION 3.3)
project(mytest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(mytest ALL COMMAND mingw32-make WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
And the custom Makefile is like this (it is located at the root of my project and generates the executable in a bin directory):
all:
g++ mytest.cpp -o bin/mytest
I am able to build the executable and errors in the log window are clickable.
Hints in the IDE are quite limited through, which is a big limitation compared to pure CMake projects...
Is there a way to specify in Xcode which binaries to use for compilation? I have recently cross-compiled clang, ld, libtool, etc, and I want to test them.
I am fully aware that I could just do something like:
/Path/To/My/Tools/clang -c file.c
/Path/To/My/Tools/ld file.o -o executable
# or
/Path/To/My/Tools/libtool -static -arch_only x86_64 -o myLib.a *.o
# and so on...
However I am looking for a way to integrate this nicely into Xcode. As my tools should (technically - this is what I want to test) be equivalent to the built-in tools, I don't need to change any of Xcode's default compilation args, etc. I just need to tell it to find the binaries some place other than /usr/bin or somewhere in the ${SDKROOT} or *.xctoochain directories. Is this possible?
There is a project setting for this. In your Xcode project, open the "Project Navigator" and click on the icon for your project. It should show the project settings. In the "Project" section of the project settings, click on the "Build Settings" tab. Type "compiler" into the search field, and it should show you the "Build Options" setting for "Compiler for C/C++/Objective-C". It defaults to Apple LLVM 5.0, but you can change it. Click on "Default (Apple LLVM 5.0)" and select "Other…" It will pop up a text field where you can enter the path to your own tools. It should look like this:
I've just downloaded Eclipse CDT developer kit (87MB) for Windows. I've also installed MinGW, and msys.
I also added this to PATH: C:\msys\1.0\bin;C:\mingw\bin. and restarted computer after that. I've checked by type "make --version" in cmd and it works.
However, for some reason I cannot compile my C project. I don't get binary files and got only the following things in COnsole:
**** Build of configuration Default for project XXX ****
make all
make: *** No rule to make target `all'. Stop.
Could some one help me with this please?
For future reference, if you're trying to import an existing project with a makefile...
This message will still pop up if your makefile doesn't have an "all" rule. Using the "Generate Makefiles automatically" option should take care of this automatically. If you don't want makefiles made for you, you have at least 3 simple options...
Option 1
If you don't want to use a rule by that name, use twokats' solution. Here's a clarification.
Go to Project Properties -> C/C++ Build -> Behaviour Tab.
Leave Build (Incremental Build) Checked.
Remove "all" from the text box next to Build (Incremental Build).
This lets Eclipse know you aren't trying to use a make target called "all". For some reason, that is the default.
Option 2
Use something similar to Etiennebr's makefile. Note, the all: $(TARGET) line is the rule that Eclipse is complaining it can't find.
Option 3
Substitute "all" with a rule name of your choice, and be sure to include that rule in your makefile.
Just for your reference, there is a way to configure the CDT build options. I had this same error message (although I did have a make target - just not named "all") and found this solution (for Galileo + CDT):
Right click your project and choose Properties. The Properties dialog will appear and you should see a C/C++ Build option where you can set specific build options. Highlight this item, and the Properties page will appear. Choose the configuration you wish to modify, and then in the section below that you should see 2 tabs: Builder Settings and Behavior. It is the Behavior tab you want. In this section you can set preferences for build settings and workbench settings, including specifying a target name (default is "all") or turning off automatic builds.
This was incredibly helpful to me when I started using the CDT. My source code is separate from the build area, and until I configure, no makefiles exist. When I configured, my default target name is explicitly "default", not "all". It was annoying to have Eclipse report an error in my project before I did anything. Setting up the environment to match my development worked wonders. HTH.
right click the project Properties->C/C++ Build, in the "Builder Settings" check the "Generate Makefiles automatically" option, and then select the "Builder type" option to "Internal builder", and then click ok, the problem was solved!
I spent a lot of time on this error and now realized that those projects that are not compiled were created before I installed MinGW and msys so there was no makefile before. And there was no include folder with link to the makefile. That's the reason why I could not compile it. Now as I create new project, it's fine.
However, I wonder if there is any way to add the path to makefile for the previous projects?
Thanks
You should take a look at your makefile (or create one if missing). That's the default makefile :
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = main.cpp
LIBS =
TARGET = main.exe
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
For example, if I need Gtk+ include paths.
How to use pkg-config gtk+-2.0 --cflags in Xcode project settings?
One option, but it's not very portable to other developers on the project -- you can just run pkg-config gtk+-2.0 --cflags in your Terminal and paste it into Build Settings -> Other C Flags. I'd be interested to hear how others deal with this in a more portable way though. Ideally, it would be nice to have pkg-config run at compile-time to make building more system-independent.
Create an aggregate target
In Build Phases, add a Run Script
#!/bin/bash
OTHER_CPLUSPLUSFLAGS="$(pkg-config gtk+-2.0 --cflags)"
echo -e "OTHER_CPLUSPLUSFLAGS = \$(inherited) ${OTHER_CPLUSPLUSFLAGS}" > MyApp.xcconfig
In Info in your project, set MyApp.xcconfig as your target configuration file
In Build Phases in your app target, add the aggregate target as a dependency
Exclude MyApp.xcconfig in your version control
One drawback is that until you build the aggregate target directly or indirectly at least once, the autocomplete will not work properly.
More portable would be to write a script that writes the output of pkg-config into an .xcconfig file and then include that in your project. Just be sure to not add it into your source repository.
Use a makefile that uses pkg-config and any other shell tools to build a lib.
Create a small, dependency-free API file for the lib that will compile in XCode, and include it and the built lib in the XCode build.
Optional: encapsulate the makefile build into a "Run Script" build step in XCode.
In addition to #keithyip's great answer, if linker flags are also needed, use
OTHER_LDFLAGS:
#!/bin/sh
OTHER_CPLUSPLUSFLAGS=$(pkg-config --cflags gtk+-2.0)
OTHER_LDFLAGS=$(pkg-config --libs gtk+-2.0)
echo "OTHER_CPLUSPLUSFLAGS = \$(inherited) ${OTHER_CPLUSPLUSFLAGS}" > MyApp.xcconfig
echo "OTHER_LDFLAGS = \$(inherited) ${OTHER_LDFLAGS}" >> MyApp.xcconfig