How to compile and link sqlite3 in a C++ program - c++

I get the following response when I try to compile and link sqlite3 in a C++ program.
I use Eclipse Mars for C++
g++ "-LC:\Sqlite\sqlite3.h" -o Stryktips.exe "src\Stryktips.o" "-lC:\Sqlite\sqlite3.h"
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../.
./../x86_64-w64-mingw32/bin/ld.exe:
cannot find -lC:\Sqlite\sqlite3.h
collect2.exe: error: ld returned 1 exit status

C:\Sqlite\sqlite3.h is a header file. And as such you should #include it, not link with it. You should link with a file ending with .lib or .a
And the -L option is to add a directory path, not a file.
So your linker command like should look something like this:
g++ -o Stryktips.exe "src\Stryktips.o" -LC:\Sqlite -lsqlite
That will tell the linker that libraries can be found in the C:\Sqlite directory, and that it should link with the sqlite library.

Related

Linking Paho C Mqtt library error in C++ Project

I'm trying to include the MQTT-C-Client-Library in a simple C++ project.
I have included the header file succesfully like this #include "MQTTClient.h". Compiling it in the linux terminal was printing this errors:
[xy#localhost mosquittoProject]$ sudo g++ *.cpp -o MQTTTest
/tmp/ccHn3s6m.o: In function `main':
mosquitto_test.cpp:(.text+0x11e): undefined reference to `MQTTClient_create'
mosquitto_test.cpp:(.text+0x13f): undefined reference to `MQTTClient_connect'
collect2: error: ld returned 1 exit status
I figured out that I need to link the library after some googling: Example MQTT Client Code not working C
Based on this question and answer I tried compiling it again like this:
sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
Which compiles fine but when running I get still an error.
Console commands and output:
[xy#localhost mosquittoProject]$ sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
[xy#localhost mosquittoProject]$ ./MQTTTest
./MQTTTest: error while loading shared libraries: libpaho-mqtt3c.so.1: cannot open shared object file: No such file or directory
I replaced the actual username by xy in this post.
What am I doing wrong here?
The problem looks like the library (libpaho-mqtt3c.so.1) is not on the library path.
It looks like you are linking against the build location of the library and have not installed it to the default system location (e.g. /usr/local/lib) by running sudo make install.
By default on Linux the runtime linker searches the locations listed in /etc/ld.so.conf and /etc/ld.so.conf.d. if you edit these remember to run sudo ldconfig to update the cache.
You can add the location of the library to the LD_LIBRARY_PATH environment variable e.g.:
$ LD_LIBRARY_PATH=/home/xy/Desktop/paho.mqtt.c/build/output/ ./MQTTTest

Boost with eclipse cannot find thread library

I have downloaded and build the boost libraries with
bootstrap mingw
and
b2 toolset=gcc
and the libraries are created in
C:\Boost\boost_1_57_0\stage\lib
In eclipse I have added this directory as a library path
and added the library libboost_thread-mgw49-mt-1_57 with and without the .a on the end.
the g++ line looks like
g++ "-LC:\\Boost\\boost_1_57_0\\stage\\lib" -o MyThread.exe main.o MyThread.o -llibboost_thread-mgw49-mt-1_57
and I get the error message
ld.exe: cannot find -llibboost_thread-mgw49-mt-1_57 collect2.exe: error: ld returned 1 exit status
If I add the .a it is appended to the error message as expected.
what am I doing wrong?
Specify the library without the lib prefix (which is the UNIX convention):
g++ "-LC:\Boost\boost_1_57_0\stage\lib" -pthread -o MyThread.exe main.o MyThread.o -libboost_thread-mgw49-mt-1_57
There's even a chance that (with the help of some standard convention symlinks) you could just say -lboost_thread.
PS Don't forget to use -pthread as well

Linking to ROOT files in xCode

I need to link to the .so files and headers found in /root/lib and /root/include in a project in xcode 3.2.6. ROOT is an analysis framework from CERN.
There is a utility root-config, that will return all the libraries necessary, I can compile on the command line using:
CFLAGS = `root-config --cflags`
GLIBS = `root-config --glibs`
test : main.cpp main.h
g++ $(CFLAGS) $(GLIBS) -g -Wall main.cpp -02 -o test
Program runs fine with no bugs. But, I want to use xcode for the whole project, but can't get it to either
A: use this utility
or
B: search the right paths to the .so files. I have included /root/lib and /root/include in the build variables header_search_paths and library_search_paths under the build settings. I then actually type in the files I need into the other_linker_flags like so:
-llibTree -llibHist -llibRIO -llibCint -llibCore
xcode returns the message:
ld: library not found for -llibTree
collect2: ld returned 1 exit status
Command /Developer/usr/bin/g++-4.2 failed with exit code 1
Does anybody know whats going on? Can xCode compile .so files? Is there some other issue here?
This has nothing to do with ROOT. To link against a library named libSomething.so in your library search path you can use a linker flag -lSomething. You want to link against e.g. libTree.so, so the correct flag to use would be -lTree, not -llibTree which would look for liblibTree.

CodeBlocks cannot find shared libraries even when search paths are setup

I have a very basic C++ project in code blocks that makes use of glfw.so and two other libraries that are compiled to .so files from another project, libHorde3D.so and libHorde3DUtils.so. The latter are placed in the project root folder, while glfw is somewhere in my /usr/lib (I think).
I have added the project folder to the linker and compiler search paths in code blocks. I have added the libHorde3D.so and libHorde3DUtils.so as well as glfw.so to the Link Libraries in the Linker Settings tab. I thought that this would be enough based on the previous similar questions here on stackoverflow.
However when I press build:
ld cannot find -lHorde3D.so
ld cannot find -lHorde3DUtils.so
ld cannot find -lglfw.so
My system is Arch Linux 64 and I am using GCC.
I also tried bopying libHorde3D.so and libHorde3DUtils.so in /usr/lib and /usr/lib64 with no success.
P.S. All search paths are copied across the Debug and Release target.
Say, if the library name is libmylibrary.so, then linker option to link against that library would look like -lmylibrary. Note that lib prefix and .so suffix are not there — they are added automatically by the linker. In your case it seems like you specified the wrong name. Try removing .so from it, that should solve the problem.
Here is a simple demonstration of how to trigger the failure by making a similar mistake:
$ echo 'int main() { return 0; }' > test.c
$ gcc -o test ./test.c -lc
$ gcc -o test ./test.c -lc.so
/usr/bin/ld: cannot find -lc.so
collect2: ld returned 1 exit status
$
The first command succeeds and the second one (with incorrect library name) fails.
You must not pass ".so". The linker options are
-lHorde3D -lHorde3DUtils -lglfw
This way the linker will search for "libHorde3D.so" etc. in the library path(s).

How do I link libcurl to my c++ program in linux?

I need to use libcurl in a piece of software I am writing on my ubuntu machine. I am using Eclipse to write and compile all of the software. When I put the libcurl files in the same folder as the .cpp file, and include the curl.h file in the header, When I attempt to compile the program, It comes up with these errors:
Building target: sms
Invoking: GCC C++ Linker
g++ -o"sms" ./src/sms.o
./src/sms.o: In function `main':
/home/geekman/workspace/sms/Debug/../src/sms.cpp:38: undefined reference to `curl_easy_init'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:42: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:44: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:46: undefined reference to `curl_easy_perform'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:47: undefined reference to `curl_easy_cleanup'
collect2: ld returned 1 exit status
make: *** [sms] Error 1
I took the contents of the include folder from libcurl, and placed them in the same folder as the .cpp file. then in the header of the .cpp file, I typed:
#include <curl/curl.h>
I also tried:
#include "curl/curl.h"
Any ideas on the problem? Thanks.
Your header file inclusions are just fine; your problem is occurring at the linking step. In order to link against libcurl, you need to add the -lcurl command line option, assuming it's installed in a standard directory:
g++ -o sms ./src/sms.o -lcurl
If it's not installed in a standard directory, you also need to add the -L/path/to/libcurl, e.g. something like:
# Assuming that /home/geekman/workspace/libcurl is where libcurl.a is located
g++ -o sms ./src/sms.o -L/home/geekman/workspace/libcurl -lcurl
Also note that the -lcurl option has to appear after the list of object files you're linking, otherwise it won't link properly.
You can try to use curl-config --libs.
An alternate answer (the first one is excellent). Consider using the output returned by "pkg-config --libs libcurl" as an argument to your compiler.
For example,
CPPFLAGS=`pkg-config --libs libcurl`
g++ $CPPFLAGS myfile.o
Pkg-config is a standard way for open source libraries to communicate to you how to link against them / #include their files.
Anyone who is using ecplise CDT then you need to do following. First on terminal enter
curl-config --libs
On my machine, the result is
-L/usr/lib/i386-linux-gnu -lcurl
then do according to this screenshot and you will be able to compile. btw don't forget to add header files in your code
So you enter library folder path without -L and library name without -l because they will be automatically added by linker.
You have to link the library to your program. With gcc (and most other compilers) you can specify the libraries to link with -lname_wo_lib, e.g. -lcurl
Also see GNU GCC Manual - Options for Linking for a detailed explanation of the options Adam Rosenfield said. For standard search directories, see An Introduction to GCC - for the GNU Compilers gcc and g++ - Setting Search Paths.
In addition to the first answer, I had to link the curlpp library too. So to compile the main.cpp file which included the curlpp I had to do:
g++ main.cpp -lcurl -lcurlpp
Using only one of the two links would return different errors regarding different links. It is important to remind that this only worked because I had installed all the necessary libraries in the standard include folders