How to use .so library in C++ with Clion and Cmake? - c++

There is a question about this already, but the asker was either doing some more complicated then me, or the posted answer assumed too much background knowledge for me to understand.
I am trying to use the following library: https://github.com/samehkamaleldin/socket.cpp
.
I downloaded the library from github and built it. I then placed the resulting .so file in the same directory as main.cpp, like this:
However, I'm not sure what to do from here, from my research, I need to use the command target_link_libraries, but I'm not sure what to pass in for each parameter.

In order to use the library, there are few approaches, I suggest you read this post: Correct way to use third-party libraries in cmake project

Related

How C++ organize(manage) third-party lib? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
(I've just edited the question to be more specific)
I know that this question might be too general to answer, but I'm just can not find good tutorial on this, so trying to seek for help here.
I'm new to C++, previously my main programing language is Java and Python. The way that C++ manage third-party lib is somehow confusing for me......While Python can easily install things with pip, Java can import the JAR, how C++ organize those things?
I would like to split my question into few parts:
Here is some understanding and question of mine:
As long as the compiler, or IDE, know the path of the lib, then everything is fine. So when saying install, we just add the path of the lib to some system path. And for IDE, we just config the setting so that it can resolve the lib in given path. Correct me if anything is wrong.
Some C++ lib are all source code, and some contains sth. like .so or .dll, what is that? And what's the difference? I saw some lib saying that it can be used with simply include a few headers, but some require static linking, what does it mean?
What's is a general good approach to manage all those lib? (For example, in python, pip will simply install to some global scope, or we use vitrual env to manage that. Then anything similar to pip in C++?
More specifically, I'm using CLion, and Clion use CMake, so maybe all I suppose to do is config the CMakeList.text correctly and then the IDE will resolve all lib and compile correctly?
Again sorry for such general and somehow opaque question, but I'm totally lost as a newb for C++, which is much more complicated than Python and Java I used before.....
Any good tutorials might be of great help, thanks!
C++ doesn't. C++ is a language not a specific compiler or implementation.
With that said, for most compilers, building a C++ application is done in multiple steps:
Edit
Compile to object file
Link to executable.
The C++ compiler is technically only involved in step 2 (and really only part of step 2).
Most compilers and linkers since long ago allow you to put header- and library-file anywhere, and then there are flags passed to the compiler and linker on the command-line that tell the compiler and linker where to find the files.
For header files the (common) command-line option -I (upper-case i) is used to add a path to be searched for header files. For libraries the option -L similarly adds a path to be searched by the linker for libraries. There are of course default paths built into the compiler and linker, and the -I and -L options adds to those defaults.
Then to link with an actual library, the linker-option -l (lower-case L) is the common option to use. Each -l options list a single library that needs to be linked into the executable.
In regards to CMake and CLion, the CLion IDE doesn't really link anything at all. Instead it uses CMake to create a set of makefiles which contains the information used to build the targets.
Lastly there are some C and C++ alternatives to PIP or other languages package managers, but generally you use the standard way to install programs and libraries on your system.
Like on Windows you find an installer, and then modify your project settings (using CMake CMakeLists.txt, raw Makefile, or IDE settings) to add the directories needed.
For Linux systems you use the standard package manager (like apt on Debian-based systems, or yum on Fedora-based systems, etc.) to find and install libraries. Then the libraries and their header files will be installed in the default locations. You still need to set up the build-environment to actually link to the libraries.
The common way is, that you include the thirdparty stuff as a .dll or you can include it direct as code (as example boost ... you have to load it and to make it work you only have to include the parts you want, and for some parts from boost you have to build it with your compiler settings and include the .dlls)
The thing with the manager like you want I only now from VisualStudio with NuGet. I have no idea if is there such a thing for CLion.
As example you can look to the example from opencv:
https://docs.opencv.org/master/d3/d52/tutorial_windows_install.html
For your questions:
Correct. But in case the lib have to match also the settings (32/64 bit, release/debug)
If you only have to include some headers, then the code is direct included to your project and compiled with your code. If you have to link it as a binary (.dll windows, .so Unix (i think please correct me if wrong)) than the code is compiled and you link the compiled functions to your code.
Here a answer to .so:
What are .a and .so files?
And here for static and dynamic libs:
When to use dynamic vs. static libraries

Using 3rd Party Libraries in C++

I'm totally spinning my wheels with getting a couple of 3rd party libraries to work with my c++ programs. I'm looking for some general advice (40,000 foot level) about the general steps that one needs to take when implementing libraries.
First, some specifics: I am using code::blocks in Windows as my IDE. I like this IDE and really don't want to switch to anything else if I don't have to (I've tried visual c++ and also some things in linux). The libraries that I am trying to use are GMP and crypto++.
OK. What I think I know is this: After downloading the library, I unzip the file to a folder. I've been unzipping directly to C:\ with each zip file extracted to its own folder (e.g. c:\cryptopp and c:\gmp). I think that the next step is to build the library, but this is where I get totally stuck. How is this done? There are no executable files among those extracted. From what I can tell, I believe that I do this in code::blocks, but I have no idea how?
Finally, assuming that I can get this done, which I believe creates the .lib files, the last step before actually using the library in my code, is to link into the library. This part, I believe that I understand.
So, my question is broad: do I understand this process overall? And if so, how do I go about building these libraries, if in fact that it the thing that I am missing.
Thanks very much for indulging my ignorance. I'm totally rudderless right now and despite hours and hours on google, I'm making no progress. Also, feel free to correct anything that I have stated as fact that is not correct. Thanks a lot!
Usually libraries have a special file called makefile in them, and are built with a utility called Make (or one of it's variations, whatever works uder windows).
Usually all you have to do is to run Make in the directory where you have unpacked the source files, and it will do the rest itself.
If those libraries you mention (GMP and crypto++; disclaimer: I'm not familiar with either of them) don't have project files for code::blocks then you may still be able to compile them under Windows with MinGW.
If you have installed MinGW you use the MinGW shell to navigate to the appropriate directories which would be /c/cryptopp/ and /c/gmp in your examples - The MinGW shell works like a Unix shell, and has different naming conventions.
Then you need to compile the libraries. Check whether there's a Makefile in those directories, if there isn't you can check whether there's a configure script, which will generate the Makefile. If you have the Makefile you can type make which will compile the libraries with MinGW's port of the GCC compiler.
When the compilation is complete you should have a library in the form of a .a file (say libcryptopp.a) that you can link to your project. In code::blocks you would set the linker path (the -L command line option in GCC) to C:\cryptopp\bin or wherever the library has been compiled, and then add libcryptopp.a to the list of libraries you want to link (this is associated with the -l option in GCC). The convention is to leave out the lib prefix and the .a extension, so you would just add cryptopp to your library list. In the end your linker options should look like -LC:\cryptopp\bin -lcryptopp along with the
Also, to be able to use the libraries you need to add the path to the headers directory to the include path of your project. This is associated to the -I command line option in GCC, so your compiler's command line options would have something like -IC:\cryptopp\include somewhere.
Of course, all of the above assumes that you use code::blocks with GCC. If you're using it with VisualC++ then the principles are the same, but the specific steps differ.

First time using boost - why did I not have to explicitly link to it when building?

In answer to one of my recent questions, it was suggested that I use a function in boost to solve my problem. This is my first time using boost so I added the following .hpp file
#include <boost/algorithm/string.hpp>
and function call
boost::replace_all(marketPrices, "\\:", "-COLON-");
to my source file, then ran make to build my application using g++ as normal.
At this point I realized I had not added any new library to the link step in my makefile and fully expected the link step to fail. To my surprise it did not fail - not only that but the code ran exactly as it should have done without any complaint.
This surprises me to say the least - how did g++ know what to link to and why did it automatically do so? Or am I missing something fundamental with the way the boost libraries operate? I know that boost uses a lot of templating and this is an aspect of C++ that I am not overly familiar with so I am wondering if perhaps this has something to do with what I am seeing.
Some of the boost libraries are header-only meaning that there is no binary to link against. Other libraries like boost::thread will require you to add a new lib to the linker.

How to build a boost dependent project using regular makefiles?

I'm working on a c++ project, and we recently needed to include a small part of boost in it. The boost part is really minimal (Boost::Python), thus, using bjam to build everything looks like an overkill (besides, everyone working on the project feels comfortable with make, and has no knowloedge of jam).
I made quite some tests already, but I cant find a way to include the formerly mentioned library in my makefile and make the build succesful.
All your help is deeply apreciated. :)
You can use Boost's bcp utility to extract only the subset of Boost you need.
That will minimize your build time & size.
That doesn't answer your question, though. You may want to consider building the Boost.Python libraries separately and checking them directly into your source control system. Then nobody would need to build them.
I had the same problem and found a solution in this tutorial. You 1) need to compile the source into an object file with the -fPIC gcc option, and 2) compile this object into a library with the -shared gcc option. Of course you have also to link against the Boost.Python library (generally -lboost_python, however for my debian system it is for example -lboost_python-mt-py25, I have also to add -I/usr/include/pythyon25). In my makefile I end up doing those two steps in one command. See also p. 13 of this presentation.
If you're uncomfortable with bjam, you might want to consider using Boost.Cmake.
Alternatively you should at least be able to see more easily what they are doing then with the bjam files.
Run bjam from the makefile, just for building that part

Trouble with boost and Code::Blocks

I've been trying to get the boost library working with Code::Blocks and am having some trouble.
When I first tried to get boost, all I did was download the latest zip file and extract it into the CodeBlocks folder. Then I set the compiler settings to look in the boost folder. This allowed me to compile, but not to link. I then read the BoostWindowsQuickReference. I removed everything I had related to boost from my machine, and started fresh.
I followed the instructions step by step, and the only thing that didn't go exactly as the instructions said was that the install-proper folder with the include\boost inside of it was in C: and not my CodeBlocks folder. So I simply copied it (just in case for some reason it needed to be there) to the CodeBlocks folder, which I thought odd because I already had a boost_1_40_0 folder there from downloading the .zip.
I then tried to compile a program and it came up with the exact same error. Then I realized that I forgot to put in the link library (Ex: boost_regex-mgw44-mt-1_40.lib). Now I get
error: ld.exe||cannot find -lboost_regex-mgw44-mt-1_40.lib|
I have a few questions:
Obviously, what am I doing wrong?
Will I need to put in a link library every time I want to use a boost facility (everything is separated into different files, there isn't just one big project.)
Was it necessary to build the library with the boost-jam or could I have just extracted it and used it? (Probably a dumb question, but a small seed of doubt was planted when I got the exact same error.)
Should I try nuwen's MinGW Distro? (Would it make things any easier?)
If any clarification is needed I'd be happy to do so. Thanks.
Edit: and now I can't compile regular programs. So I'm just starting fresh again.
1, it should be -lboost_regex-mgw44-mt-1_40
2, Read the document, most boost library doesn't require to link library
3,4, You should compile it yourself, or try nuwen's MinGW ( I'd installed it and it worked fine )
BoostPro has Windows binaries available for the Boost libraries. If you download just the Boost sources, you will have to compile it, if you are using any of the libraries that are not header-only (such as boost regex). The BoostPro binaries will allow you to link to these without having to build anything.
On Windows it doesn't really matter where you "install" Boost to. Just get the .7z, compile using bjam.exe and pass it the options you need. It will create a folder called "bin.v2" and put the resulting libs in there. In Code::Blocks, all you need to do is edit the project options and point the search path to boost_1_40_0\boost and manually input the libraries to link against (those from bin.v2). It should just work then.
Do not use a precompiled Boost library.