How to add library curlpp to C++ project [duplicate] - c++

This question already has an answer here:
How do I add curlpp to my project?
(1 answer)
Closed 4 years ago.
I want to add curlpp to my C++ project. Currently, I have a main.cpp file which looks like this:
#include <iostream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
int main() {
return 0;
}
I compile using:
"g++ -std=c++14 -I/usr/nguyenthesang/Desktop/myprogram/curlpp-0.8.1/include main.cpp" and it successfully compiles.
Then I add the implementation in the main function (the below is copied from curlpp's repo):
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
using namespace curlpp::options;
int main(int, char **)
{
try
{
// That's all that is needed to do cleanup of used resources (RAII
style).
curlpp::Cleanup myCleanup;
// Our request to be sent.
curlpp::Easy myRequest;
// Set the URL.
myRequest.setOpt<Url>("http://example.com");
// Send request and get a result.
// By default the result goes to standard output.
myRequest.perform();
}
catch(curlpp::RuntimeError & e)
{
std::cout << e.what() << std::endl;
}
catch(curlpp::LogicError & e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
When I compile by using "g++ -std=c++14 -I/usr/nguyenthesang/Desktop/myprogram/curlpp-0.8.1/include main.cpp" , there are compilation errors, which say that "ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)".
The errors may come from the fact that I have only linked the header files to the program but not the library itself. I google around to find the way to link the library (for example using -L options) but it did not work. I need help with this problem.
I also want to ask that is there a general way for adding every library into C++ project, like Cocoapods in iOS?
I appreciate your help.

On ubuntu I had success with installing these packages:
sudo apt-get install pkg-config libcurlpp-dev libcurl4-openssl-dev
(pkg-config is to be used in CMakeLists.txt to find the curlpp and set an env var that points to it)
Then in my CMakeLists.txt I added:
include(FindPkgConfig)
pkg_check_modules(CURLPP REQUIRED curlpp)
Then still in CMakeLists.txt add to target_link_libraries ${CURLPP_LDFLAGS} for example:
target_link_libraries(mylibcurlppprog ${CURLPP_LDFLAGS})

Related

[C++ Library]: "file or folder does not exist" when compiling

I want to develop a small application which uses some libraries. So I downloaded them and placed the include files in a folder called include.
For my application, I used cpprestsdk, but my question shouldn't be limited only to this library.
This is a rough example of my folder Structure:
myproject
include
cpprest
...
pplx
...
test.cpp
And this is my Code:
#include <iostream>
#include "include/cpprest/http_client.h"
#include "include/cpprest/filestream.h"
#include "include/cpprest/json.h"
int main() {
// code
std::cout << "Hello World!";
return 0;
}
which results in followin error code when compiling (g++ test.cpp -o test) with g++ or gcc (on Ubuntu):
<error needed>
What have I done wrong? when I inspect the file mentioned in the error message, then I notice, that all includes in the library are like so #include "cpprest/asyncrt_utils.h". As you can see, it refers to the file as it were in a subfolder called cpprest, which it is not. It is located with the other file in the same folder. I guess that results in my problem. My question now is: how do I fixe this issue?

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

How to link Qt codes to C++ codes (or C) in Mac using Macports?

I have installed Qt using Macports. However, I do not know how to link Qt into the code ... what is the exact command to compile the code, the include path, library path and the name of the library. Could any one help me with this? I use clang and g++ as the main C++ compiler.
#include <Qdebug>
#include <iostream>
int main()
{
cout << "Test compilation.";
return 0;
}

Linker error when linking C++ code with USB driver (ftdi)

I am on a Mac OSX and I am trying to start using the C ftdi drivers I installed using brew
brew install libftdi
That installed the library in the directory /usr/local/Cellar/libftdi/1.2 And I have the following C++ code
#include <iostream>
#include <ftdi.h>
using namespace std;
int main() {
struct ftdi_context ftdi;
ftdi_init(&ftdi);
cout << "Hello World" << endl;
return 0;
}
How should I compile this to make it work? I am including the include/ directory so the compiler can find the header file but I do not know which object file should be linked. I am trying to using the following command
g++ -I /usr/local/Cellar/libftdi/1.2/include/libftdi1/ /usr/local/Cellar/libftdi/1.2/lib/libftdi1.a test.cpp
But I am getting bad linker errors even though I have included the .a file. The lib/ directory has the following contents
cmake
libftdi1.2.2.0.dylib
libftdi1.2.dylib -> libftdi1.2.2.0.dylib
libftdi1.a
libftdi1.dylib -> libftdi1.2.dylib
pkgconfig
Is there something else I should try and link it to? Even if you are not aware of this particular driver is there something in general that I should be doing for drivers like this that I have not done?
Thanks for the help!

Include paths not found while compiling with g++ on MacOS

I'm trying to compile the simplest program on MacOS 10.6 like:
$ g++ -o hello hello.cpp
the following source:
#include <iostream>
int main (int argc, char * const argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
I'm getting the error:
hello.cpp:1:20: error: iostream: No such file or directory
hello.cpp: In function ‘int main(int, char* const*)’:
hello.cpp:4: error: ‘cout’ is not a member of ‘std’
So obviously I have to add the include path somewhere. My question is where can I find the include directories and how can add them globally (I don't want to provide the include path whenever I want to compile).
I just installed the XCode 3.1.4 and managed to compile it via Xcode, but not via command line. I found some header files in this directory:
/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers
and tried to add it to the HEADER_SEARCH_PATHS after reading this question, but no luck.
I'm developing on Linux and everything is working fine there, but I want to continue doing that on MacOS. Any help?
On my Mac, that include file is in /usr/include/c++/4.0.0/iostream . Are you sure
you have all the command-line development tools installed? They might not be by default;
I'm pretty sure I had to install it manually when I first set up my Mac. There should be a "developer tools" package somewhere on your OS X installation media.
Or, if you want to make sure you're getting the latest version, you can download it from:
http://developer.apple.com/technology/xcode.html
$ g++ -o example.bin example.cpp //to compile
$ ./example.bin //to run
It's code:
#include <iostream>
using namespace std;
int main () {
cout << "Hello, World!\n";
return 0;
}