Linking flags order using QMAKE in QTCreator .pro file - c++

I'm getting
[...]main.cpp:9: error: undefined reference to [...]
I already know the cause, but, I can't resolve it using QMAKE_ flags.
I need to set the -I flag in linking after the -L and before the -o flag, like this (I tested a handmade Makefile, and it's OK):
-L ./lib -lm [... other libs ...] -I ./include -o ./bin/program
But setting -I in QMAKE_LFLAGS, the -I flag goes in the beginning of g++ command.

Similar solution:
Referencing C functions in static library from C++
To instance a C lib from C++, you need to surround c headers with extern "C" { }

Related

mingw g++ is unable to link with libraries

I'm trying to use X86_64-w64-mingw32-g++ (packaged in Archlinux's MingW package) to cross compile some C++ code into an Windows executable, however I'm having trouble getting past some issues.
I'm calling
x86_64-w64-mingw32-g++ -o build_win/asm build_win/asm.o build_win/asm_lib.o build_win/socket_boost.o -I../extra/etc -fopenmp -lrt -std=c++11 -g -lboost_system -lboost_serialization
from a makefile, but I get thrown the errors:
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lrt
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_system
/usr/lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_serialization
This works fine with native g++, so exactly do I have to change for mingw to compile?
EDIT: I have mingw-w64-boost package installed, which includes boost libraries pre-compiled and ready to be linked. However, it seems the naming convention is a bit different, and -lboost_system for example becomes -llibboost_system-mt (not exactly sure what the -mt suffix entails).
Problem is I can't find the mingw counterpart for -lrt. I've tried with both -lrtm and -lrtutils but in both cases I get:
[...]
undefined reference to `__imp_getsockopt'
Are you sure that -lboost_system and other libraries are present in the same directory as makefile ?
If not then please include -L flag which indicates the location of your library.
For example:
-L /path_openmp -fopenmp -L /path_boost_system/ -lboost_system -L /path_serialization -lboost_serialization
Moreover, you need not include -I and -g flag when creating an executable from .o files. These are needed when you create .o from .cpp files.
There is no rt library on Windows.
You are missing -lws2_32.
$ x86_64-w64-mingw32-nm -A /usr/x86_64-w64-mingw32/lib/*.a 2>/dev/null | grep getsockopt | grep " T "

Use Crypto++ in personal project on Windows

I have a small project to create in a course at my University that requires using the Crypto++ libraries. The requirement is that we don't include the whole source code/binary files of Crypto++ but link it from an outside directory. (E.g. C:\cryptopp). This is because the reviewer will link his/her own directory to asses my code.
Now, I am really bad at creating Makefiles and don't understand the content of them completely.
I am using MinGW on Windows 7.
So my main question would be, what do I need to write in the Makefile to use Crypto++ in my project from an outside folder?
Suppose you have the following makefile:
unit.exe: unit.o
g++ unit.o -o unit.exe
unit.o: unit.cc unit.h
g++ -c unit.cc -o unit.o
In order to modify it to use an external library you have to use the GCC -I and -L options:
unit.exe: unit.o
g++ unit.o -o unit.exe -L /c/cryptopp -l ws2_32 -l cryptopp
unit.o: unit.cc unit.h
g++ -I /c/cryptopp -c unit.cc -o unit.o
Often a makefile would contain a variable that is passed to the compiler and a variable that is passed to the linker, for example CFLAGS and LDFLAGS. If that is the case, then it might be easier to add the "-I" and "L" options to the compiler and linker variables.
See also here for a way to comiple CryptoPP.

CUDA: Creating object with G++ linker and cuda object files

I compiled my normal cpp files to .o, and my cuda .cu files to .co
I want to link these so I can call the cuda stuff from my normal C++ file
Here is my linker command that Make is running:
g++ -I ../readers/ -I../writers -I../common/ -I /home/dcole/software/xerces-c-3.1.1/src -I /home/dcole/NVIDIA_GPU_Computing_SDK/C/common/inc/ -I /usr/local/cuda/include -O3 -fPIC -fopenmp -DFIX_PAULI -DFIX_YAMA -DUSING_CUDA -o YamaguchiGPU YamaguchiMain.o YamaguchiDecomp.o cuYamaguchi.co -L/usr/lib64/ ../../lib/IDT.a ../../lib/Linux/libxerces-c.a ../../lib/Linux/libfftw3f.a -lcurl -lidn -ldl -lssl -lm -lpthread -lcuda -L/usr/local/cuda/lib64/libcudart.so
So I am definitly linking cuda, and cudart, but yet I get this:
cuYamaguchi.co: In function `__sti____cudaRegisterAll_46_tmpxft_00003190_00000000_4_cuYamaguchi_cpp1_ii_init_gpu()':
tmpxft_00003190_00000000-1_cuYamaguchi.cudafe1.cpp:(.text+0x1b4): undefined reference to `__cudaRegisterFatBinary'
tmpxft_00003190_00000000-1_cuYamaguchi.cudafe1.cpp:(.text+0x20f): undefined reference to `__cudaRegisterFunction'
The Cuda file is being linked without problems but it looks like you might have forgotten to define some necessary function (like cudaRegisterFatBinary).
Upgrading to gcc 4.7.0 or higher should fix this horrid linker issue.
I was using an absolute search path in the linker to _libcudart.so_. I changed the -L to point to just the folder, then added -libcudart so it would search the folder for that lib.

compiling a c++ program including mysql

I'm new to gcc, and trying to compile a c++ program which includes mysql.h using the command:
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
It works without issue, but I was wondering if someone could explain the arguments to me. I don't like using commands I don't understand.
Thanks
-o test means the output file is to be named "test".
test.cpp is your source file, of course.
-L/usr/include/mysql means to look for libraries in /usr/include/mysql, as well as in the usual link path. (It probably isn't finding any libraries here; my libmysqlclient.a is in the standard library directory /usr/lib. So I don't think you need this option.)
-lmysqlclient means to link with the mysqlclient library (actually named libmysqlclient.a)
-I/usr/include/mysql means to look for #include files in /usr/include/mysql, as well as in the usual include path.
try "man g++" for a full description of what the various options mean.
man gcc will give you the details of all these options.
g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
g++ : the compiler
-o test : name the resulting binary "test"
test.cpp : your source file
-L : the directory to look in for libraries (that are specified by -l)
-l : named library to link against (looks for it in -L)
-I : the directory to look in for #included header files

Aid in building boost asio ssl example

I have been working through the asio ssl examples (linked below). Despite by best efforts I have been unable to link openssl into the boost example. The output from ld is that ld is missing symbols from libssl.a. The thing that I can not figure out is that I found all the symbols in libssl.a with nm that ld says are missing. I suspect I am doing something dumb but I am not familiar enough with c++ to fix it. I have also included my makefile. The source of ssl-client.cpp is verbatim from the link.
http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/example/ssl/client.cpp
INCLUDES = -I /usr/local/boost_1_41_0/ -I /opt/local/include/
LIBS = -L/usr/local/boost_1_41_0/lib/libboost_system.a \
-L/opt/local/lib/libcrypto.a \
-L/opt/local/lib/libssl.a
CPP = g++
build: ssl-client
ssl-client: ssl-client.cpp
$(CPP) $(LIBS) $(INCLUDES) ssl-client.cpp
I think you've misunderstood how the -L option works. -L specifies a path in which to search for libraries. To specify an individual library to link to, use the -l option and omit the "lib" prefix, as follows:
LIBS = -L/usr/local/boost_1_41_0/lib -L/opt/local/lib \
-lboost_system -lcrypto -lssl
Also, there is usually no space between the -I include path option and the actual path. I'm not sure if a space in there causes problems, but you might try this to be on the safe side:
INCLUDES = -I/usr/local/boost_1_41_0/ -I/opt/local/include/
Also, as noted in my comment, you defined the LIBS variable but then used the LIB variable. The call to g++ should be as follows:
$(CPP) $(LIBS) $(INCLUDES) ssl-client.cpp