Question

I have a cpp file which I am able to compile from command line using:

 clang++ main.cpp -O2 -larmadillo

or by using g++ compiler via

 g++ main.cpp -O2 -larmadillo

My question is how can I compile main.cpp in Xcode 5.1.1?

Here is what I tried:

1) Project -> Build Settings -> Other C++ flags -> added '-O2 -larmadillo' there

2) Target -> Build Settings -> Other C++ flags -> added '-O2 -larmadillo' there

Neither helped.

I am new to this linking/compiling magic so I don't know where to go from here.

Was it helpful?

Solution

clang++ and g++ can act as both compilers and as compiler drivers. When you run them from the command line as a compiler driver they internally handle running all the steps for getting an executable, including compiling, assembling, and linking.

But when Xcode is doing the build it instead expects to run clang as a compiler and to handle linking separately. So passing a linker flag in 'Other C++ flags' is basically like doing:

// compile step (no linking)
clang++ main.cpp -O2 -larmadillo -c

// linking step (error, missing library)
clang++ main.o

So what you need to do is change the build settings so that Xcode knows that it needs to link in that library when it gets to the linking step. A quick and dirty way would be to add the linker flag to 'other linker flags' (and to set the library search paths appropriately), but it would be better to edit your target's 'Build Phases" and update the "Link Binary with Libraries" with the appropriate library. You may also need to change the SDK in the target's build settings to 'Current Mac OS', so that Xcode will use whatever libraries you've installed rather than strictly limiting it to what's available in a default OS X install.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top