Trouble with the compilation using libcurl on Windows XP - c++

I downloaded the libcurl curl-7.34.0-devel-mingw32.zip from http://curl.haxx.se/gknw.net/7.34.0/dist-w32/curl-7.34.0-devel-mingw32.zip.
I use Eclipse Kepler with MinGW as the toolchain (GCC compiler). I created html.cpp and then test the code from the example http://curl.haxx.se/libcurl/c/simple.html:
I included the headers:
#include<iostream>
#include<curl/curl.h>
using namespace std;
There are 5 cURL functions. Only 2 of them (curl_easy_init,curl_easy_setopt)are said to be 'undefined reference'.
The compiler message:
Info: Internal Builder is used for build
g++ -O3 -Wall -c -fmessage-length=0 -o html.o "..\\html.cpp"
g++ -static-libgcc -static-libstdc++ -o HTML.exe html.o
html.o:html.cpp:(.text.startup+0x12): undefined reference to `_imp__curl_easy_init'
html.o:html.cpp:(.text.startup+0x1e): undefined reference to `_imp__curl_easy_setopt'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: html.o: bad reloc address 0x1e in section `.text.startup'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
The other 3 functions said to be fine:
curl_easy_perform
curl_easy_strerror
curl_easy_cleanup
I have tried to set to properties to tell the MinGW linker to include the headers and library files. The same situation as the above or the compiler simply says the .a files not found. Adding -lcurl or -DCURL_STATICLIB to the linker's flag is useless as well.
I have also tried to copy the libraries(.a files) and the headers(.h files) of libcurl to the folders bin, include and lib inside C:\MinGW and C:\gcc-4.8.1. It turns out to be no change.
Sadly, the first challenge of using cURL is not coding but the compilation. How can I solve this problem?
Thanks you guys!

Related

libgtest.so error adding symbols: DSO missing from command line

I have been using gtests for unit testing a personal project. Last week I upgraded to the LTS version of Linux Mint. Unfortunately, after that event, I haven't been able to compile my project due to gtests linking problems.
The following error is being issued:
/usr/bin/x86_64-linux-gnu-ld: build/tests/policies/roundrobin_tests.o: undefined reference to symbol '_ZN7testing4TestC2Ev'
/home/myuser/Documents/googletest-release-1.8.0/googletest/libgtest.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I have generated both libgtest.so and libgtest_main.so through CMake 3.10.2. The gtest version is the release-1.8.0, the same I used to have before.
Those libraries are located in /usr/lib and the corresponding include folder has also been copied to /usr/include. I made sure that this location is in my ld path and that is not the problem.
My compiler is g++ v7.3.0 and the command Im using to compile the testes is:
g++ -std=c++14 -Wall -O3 -Iinclude build/tests/policies/roundrobin_tests.o -lgtest_main -pthread -o bin/policies/roundrobin_tests
I have tried altering the order of the elements in the command, explicitly adding -L/usr/lib and -I/usr/include without luck. A funny fact is that if I take off the -pthread flag, the error is still the same.
The same command was used before and the only difference is the compiler version I am using now as I used g++ 5.4.0 before. Any insights on how to solve this?
edit: Just tested the same process with g++ 5.4.0 and CMake 3.5 and the same problems ocurred.

undefined reference to `shm_open' already with -L /lib -lrt -lpthread

I just want to use the boost library to create a shared memory on an ARM system. It work fine if you want to compile it only under ubuntu. However, when I want to cross compile it with TI's CCSv6 and angstrom toolchain, it keep pushing errors.
Because I do not know how to write a makefile for cross compile, I think using TI their own IDE might be a good choice to avoid further problems.
Here is my code and print out of build console.
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
int main()
{
shared_memory_object shdmem{open_or_create, "Boost1", read_write};
shdmem.truncate(1024);
mapped_region region{shdmem, read_write};
}
g++ -std=c++0x -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -L /lib -lrt -lpthread -fPIC
The IDE called Code Composer Studio has cross compile settings as below:
Prefix: arm-angstrom-linux-gnueabi-
Path: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/armv5te-angstrom-linux-gnueabi
Build Console:
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference toshm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference toshm_open'
collect2: ld returned 1 exit status
make: *** [test] Error 1
undefined reference to shm_open' means it cannot find -lrt for ARM.
In your build command line you need to specify include and library paths to ARM built libraries, not to Ubuntu ones. So -I/usr/include and -L /lib is wrong.
Also you need boost built for ARM, although if you just want to use interprocess library then boost headers should be enough. But you need to copy them into different location because including them from /usr/include includes also other headers specific to Ubuntu.
You can use the cross compiler IDE you mentioned or arm g++ cross compiler which you can install by:
sudo apt-get install g++-arm-linux-gnueabihf. Some headers and libraries for ARM will be installed too.

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

Compile C++ and OpenSSL on Ubuntu 11.10

I got a serious problem compiling my C++ and OpenSSL project on my Ubuntu 11.10.
The compiling command is:
g++ -Wall -lssl -lm -lcrypto -I ./src ./src/server.cpp -o ./bin/server
I receive these errors:
server.cpp:(.text+0x8ff): undefined reference to `RSA_new'
server.cpp:(.text+0x92d): undefined reference to `PEM_read_RSAPrivateKey'
server.cpp:(.text+0xa85): undefined reference to `RSA_size'
server.cpp:(.text+0xaa1): undefined reference to `RSA_size'
server.cpp:(.text+0xae7): undefined reference to `RSA_private_decrypt'
server.cpp:(.text+0xd84): undefined reference to `BF_set_key'
server.cpp:(.text+0xf1d): undefined reference to `BF_ecb_encrypt'
server.cpp:(.text+0x13c6): undefined reference to `BF_ecb_encrypt'
collect2: ld returned 1 exit status
make: *** [server] Error 1
I successfully installed openssl and libssl-dev but the problem persists.
I tried to compile the project on Linux Mint 12 with the kernel 3.0 and I had the same problem.
On my old Linux OS with the kernel 2.6 the project compiled and worked fine (using the same Makefile and the same sources).
Please help me!
Generally you need to have the -l link flags after the code that references them. Try
g++ -Wall -I ./src ./src/server.cpp -o ./bin/server -lssl -lm -lcrypto
As the comment to this answer states, the linker only looks for undefined symbols to include in the order the parameters are listed.
That is, if your cpp file uses the libraries, the libraries have to be listed after the cpp file.
Those error are from crypto library, check whether ssl and crypto libraries are available in /usr/lib or where ever u installed if not install them and have u set the library search path for libssl and libcrypto in your compiling command?

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