Compile XCode 5 project with custom flags - c++

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.

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.

Related

C++ header files are not being found

Context: I am building some code for an embedded board. It calls for installing the Xilinx tools, the Linaro toolchain and then invoking a setup bash script in the development board build directory (let's call it setup.sh).
If I do not run setup.sh , I can then build one of the lower level libraries, which has it's own configure script (./configure) which calls the usual ./bootstrap script. In this scenario ./bootstrap , g++ in this case says it can find sstream (the C++ stream header). Ok. Fine.
When I run the setup.sh script (at the top level), g++ then says it CANNOT find sstream. So somehow the g++ environment is changed somehow, and it is this I am trying to figure out.
The output in this erroneous case is
g++ has streams in std:: namespace
g++ does not have sstream
g++ does not have strstream.h
g++ does not have strstrea.h
I am trying to debug this to find out what g++ thinks it is doing and why it cannot find the sstream header. What are some ways to look at what the g++ include and libstdc++ library paths are set to? What environment variables control the behavior of gcc/g++?
#
If I remember correctly, Xilinx ships their own gcc toolchain with their products.
Just add the -I compile file to point to the proper includes path.
Try to search for the header files inside Xilinx's /opt path, so you don't have a mismatch between the header version and the library version.
Makefile's usual env variables are:
# C Compiler: GNU C Compiler
CC = gcc
# Linker: GNU Linker
LD = ld
# C++ Compiler: GNU C++ Compiler
CPP = g++
Also check
CFLAGS
CPPFLAGS
LDFLAGS
Check if $CC is set after you execute setup.sh.

Add C++ Library to eclipse C++ Project

I am attempting to add an existing library to Eclipse. I use a cross compiler for C++ with the Eclipse IDE, installed on a virtual linux debian machine.
The mmapGpio lib is found here.
/mmapGpioBasicRev1.tar.gz has a cpp and an h file with a small demo program.
I have compiled this code without a problem. A .o file is generated. I've archived the file successfully with ar -q libmmapGpio.a mmapgpio.o
I've placed my libmmapGpio.a in ~/.../UserLib directory
I've placed my mmapGpio.h in ~/.../UserInclude
At this point all is OK.
I open a new project that uses the mmapGpio library:
#include "mmapGpio.h"
#include "stdio.h"
int main(void){
mmapGpio rpiGpio; // instantiate an instance of the mmapGpio class
rpiGpio.setPinDir(17,mmapGpio::OUTPUT); // set GPIO17 to output
while(1) {// toggle pin as fast as possible
rpiGpio.writePinHigh(17);
rpiGpio.writePinLow(17);
}
return 0;
}
So cross-compilation is done, but linker say cannot find -llibmapGpio!
I have made declaration in the properties project; C/C++ General
includes path : /home/octopuss/rpi/UserInclude (the mmapGpio.h file)
Library path : /home/octopuss/rpi/UserLib (the libmmapGpio.a file)
Libraries : libmmapGpio
Why do I receive this message?
for detail -> console view
03:16:30 **** Build of configuration Debug for project Gpio1 ****
make all
Building file: ../Gpio1.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/home/octopuss/rpi/UserInclude -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Gpio1.d" -MT"Gpio1.d" -o "Gpio1.o" "../Gpio1.cpp"
Finished building: ../Gpio1.cpp
Building target: Gpio1
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/home/octopuss/rpi/UserLib -o "Gpio1" ./Gpio1.o -lmmapGpio
/home/octopuss/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: skipping incompatible /home/octopuss/rpi/UserLib/libmmapGpio.so when searching for -lmmapGpio
/home/octopuss/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lmmapGpio
collect2: error: ld returned 1 exit status
make: *** [Gpio1] Erreur 1
error :
skipping incompatible /home/.../UserLib/libmmapGpio.so when searching for -lmmapGpio
ld: cannot find -lmmapGpio
"why this message ?"
It's because with your settings, the linker actually looks up for a library file named liblibmmapGpio.a.
"... so crosscompilation is done but linker say "cannot find -llibmapGpio" !
...
- Libraries : libmmapGpio"
You just need to specify the library without the lib prefix in the linker library settings:
mmapGpio
The Eclipse CDT Builder passes this as a -l option to the linker, which automatically extends to search for libmmapGpio.a at the specified additional pathes.
See also this Q&A for more illustrated samples and links:
Problems importing libraries to my c++ project, how to fix this?
I found the problem ... my .so lib wasn't ARM cross compiled so there is a X86 library not compatible whith my ARM Programm.
I solve this to set erm-linuxgnuabihf- prefix and his path to cross setting parameter.
Thanks to TTAVAR PEI and Scott Stensland
enjoy

How can CDT project be compilable by itself, but not compilable as linked folder?

I have C++ project which is compilable (the project is leptonica). It consists of configure script and then compiles with make.
If I import this project into Eclipse CDT, then it also compiles (with autotools toolchain).
But then, if I create empty CDT static library project, and refer project above from within it, then it becomes not compilable. It tries to compile referred project and produces some bad errors like include file not found and so on.
My question is how it can be?
UPDATE
Sorry, I am referring the project by linking a folder.
I.e. Project Properties -> C/C++ General -> Paths and Symbols -> Source Location -> Link Folder -> Link to folder in the file system
When linking, a message
Location 'mylocation' may overlap another resource. This can cause unexpected side-effects.
appears.
UPDATE 2
Actual error is
Building file: <mypath>/leptonica-1.69/src/freetype.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"leptonica-1.69/src/freetype.d" -MT"leptonica-1.69/src/freetype.d" -o "leptonica-1.69/src/freetype.o" "<mypath>/leptonica-1.69/src/freetype.c"
In file included from <mypath>/leptonica-1.69/src/freetype.c:31:0:
/usr/include/ft2build.h:56:38: fatal error: freetype/config/ftheader.h: No such file or directory
compilation terminated.
make: *** [leptonica-1.69/src/freetype.o] Error 1
I.e. it can't find file which it finds successfully if compiled standalone.

Autotools - passing optional profiling library to configure script

I have built and installed a shared library to do some profiling of my projects via code instrumentation, specifically with the -finstrument-functions switch of gcc.
It is possible to turn the instrumentation on and off with the compiler switch, and hence I'd like to be able to turn the dependency to the profiling library on and off just by reconfiguring.
I can pass -finstrument-functions to gcc via configure's CFLAGS, but when I try to pass -lmylib via LDFLAGS configure fails with
configure:2796: checking whether we are cross compiling
configure:2804: gcc -o conftest -g -Wall -Wextra -Werror -lmylib conftest.c >&5
configure:2808: $? = 0
configure:2815: ./conftest
./conftest: error while loading shared libraries: libmylib.so.0: cannot open shared object file: No such file or directory
The file exists, I have double checked and compiling and linking a dummy example works as expected.
Interestingly, when I pass the full path to the shared library instead of -lmylib, the error persists, but when I pass the full path to the corresponding static library, configure runs smoothly, and everything is built as expected.
My question in a nutshell: Is there a possibility to pass optional shared libraries to autotools-generated configure scripts, without changing configure.ac?
Thanks,
Andy
It looks like your library is not in the compiler's default library search path.
You may also pass library search path through LDFLAGS with the -L switch:
CFLAGS=-finstrument-functions LDFLAGS="-lmylib -L/path/to/mylib" ./configure ....
fixed by running sudo ldconfig
I'll leave this here in case someone else has the same problem.

Building shared library with Eclipse CDT directly

I would like to move the building of my C++ project completely to Eclipse CDT, however I am facing some configuration problems. Here is my old g++ compiler call:
g++ -I/home/lib/tinyxml
-I/usr/lib/jvm/java-6-openjdk/include
-L/usr/local/lib -L/home/konrad/tinyxml
-lboost_system
-lboost_thread
-lboost_regex
-fPIC
-shared
-o libagent.so
agent.cpp AgentSocket.cpp ThreadInfo.cpp
/home/lib/tinyxml/tinyxml.cpp
/home/lib/tinyxml/tinyxmlerror.cpp
/home/lib/tinyxml/tinyxmlparser.cpp
/home/lib/tinyxml/tinystr.cpp
When creating the project I choose Shared Library > Emtpy Project
Here are my problems:
The Eclipse CDT generates the makefile in a way, it first compiles every .cpp file and then recompile it to the shared library. This let's me face one or more issues. I would like to jump this step and run it in the same way I did in the console.
I cannot configure -L and -l options into the C++ building configuration, as Eclipse CDT offers these option only for the C++ linking part, but not for the C++ compiler part, but I need them already there, as the project doesn't compile without errors.
That's not a problem, but actually how makefiles usually are constructed. This way, if you change one source file, you don't have to recompile all other source files, but only the one that has changed. It minimizes recompilation time.
That shouldn't be a problem, because they are options that are only used during linking. Compilation of source files into object files shouldn't depend on external libraries.