First steps to adding a static library to c++/ROS (CMake) - c++

I'm trying to add a static library to my C++/ROS project and having trouble wondering where to start. I have the project currently working with a CMake file to build it and have downloaded (and can build) an external static library that I want to use in the project.
I am very new to CMake and C++ in general and am working on a project that I haven't created myself. I've read a lot of tutorials on how to add libraries but I'm stuck at knowing where to start as I know there are many different steps needed. I was looking at find_library(), add_library() and ExternalProject_Add() but I don't really understand the difference and the documentation doesn't make it much clearer. As it stands the CMake file has very little in it so I was wondering what exactly I need to add and what commands are best just to be able to use this library in my project. Specifically, I guess what set of commands would I add to a pretty empty CMake file for it to work given I have the lib.a file. Sorry for the vagueness but any help would be appreciated.

Related

CMake specifying Native Build System Options in CMakeLists.txt(managed C++ Project)

i got stuck with a problem which has to do with a managed c++ Project.
Currently we have a working Build and we are about to use CMake to generate our Solution Files in the future.
In our solution we have some Managed C++ Project and C# Projects. I tried to generate/build the Managed C++ Projects and it works. But there is one key thing that is different which atually breaks the build.
In our working build the managed C++ Project has the Xml Tag <TargetFrameworkVersion> but when I use CMake to configure/generate the Project I end up having <TargetFramework> and this breaks the build ( I confirmed by manually changing the Tag).
I got stuck with this Problem for 2 days and the following things I already tried:
I tried setting VS_DOTNET_TARGET_FRAMEWORK_VERSION which actually is deprecated but makes sense because of the name. I ended up having in the Project file.
I also tried the newer Variables DOTNET_TARGET_FRAMEWORK, DOTNET_TARGET_FRAMEWORK_VERSION but I ended up again with in the Project file.
I thought to myself I could change the Project file after I configure/generate but thats pretty hacky (but works)
There is a second solution which actually works but is not what I really want. I can tell cmake.exe to pass some options to the native MsBuild tool to build the managed C++ target. This works but I would prefer to some how tell the CMakeLists.txt to do this or even better have the right Xml tag when generating the Proect files because we don't want to use the command line when building our projects.
The approach looks like this (Target Name is NotifierLib and .NET Framework Version is 4.7.2)
cmake --build . --target NotifierLib -- /p:TargetFrameworkVersion=v4.7.2
Did anyone came across this exact issue? Or maybe has an idea how to solve this via CMakeLists.txt?
Thanks in advance.
I finally came to a pretty Solution which was there initially.
This post helped my to get on the right Track:
How can cmake add custom entry in a project's vcxproj PropertyGroup?
Basically I don't need to iterate over the .xml File myself to edit the Tag.
I can tell CMake to set it for me as a global option with:
set_target_properties(NotifierLib PROPERTIES VS_GLOBAL_TargetFrameworkVersion "v4.7.2")
This way the initial <TargetFramework> Tag is still in the .xml File but it does not bother because it is not being used.

Trouble installing C++ libraries from Github

I've used C++ for several courses in university but the libraries we used in these courses were quite simple to install. When we came across libraries which required a bit more complex installation, our teachers always provided detailed instructions.
This time I'm doing a project on my own. I have downloaded libraries for my own projects in the past. Sometimes the install was easier, sometimes I had to search for hours to do things most people here would do in 5 minutes.
I did some studying to remember some stuff about the compiling process, what dynamic and static libraries are, etc. I feel I understand the most part but it didn't really help me with my efforts to install some libraries. Some people in Github provide instructions for newbies like me, but others, understandably, don't.
I'm saying a lot so I'll get to the point. I will provide links for the library I'm trying to install but in case I'm not allowed to, please let me know so I can rewrite this in a way that is allowed.
I'm trying to install libccd so I can then install fcl. In the instructions 3 ways are mentioned:
Using Makefile
Using autotools
Using Cmake
In all 3 methods, I see specific commands like this :
First of all, where am I supposed to write these commands?
I don't even know how to search this. So, I tried installing with methods I've previously used.
Here are the steps I took:
Downloaded the whole repository.
Made a solution for Visual Studio with Cmake-gui (that's all I know how to do with Cmake,unfortunately it usually was enough so I never learnt more)
Compiled the code in Release mode, which should give me a lib file.
Now,in my own project, I added the relevant include directories of the repository I downloaded and the library directory for the lib file which was produced.
I'm not sure about my last step. I'm pretty sure if I did the installation as proposed in the link(and as required by fcl), I would have a new folder for the library including just an include folder and a lib folder. Now I'm just searching for the correct include files and the lib file in subfolders of subfolders hoping I include the right ones. I'm a bit lost.
I'd really like some general steps(if there are) to installing a library. I know each library has its own ways but I assume the general idea with Cmake or Makefile should be the same. I have tried searching online but I didn't come across a good or detailed enough tutorial. I really don't wanna waste any more days trying to install libraries and I don't want to end up asking here again.
UPDATE:
It looks like there is no standard way to install libraries. Since I'm getting 'close votes' I'll include a specific question:
Are the steps I took correct? What should I do next?
I'm trying to install libccd so I can then install fcl. In the instructions 3 ways are mentioned:
Using Makefile
Using autotools
Using Cmake
In all 3 methods, I see specific commands like this :
First of all, where am I supposed to write these commands?
These instructions are for a UNIX-like system. Makefiles, autotools... these are UNIX things. You'd typically write those build commands into a console window on something like Linux or a Mac.
CMake is a bit more cross-platform. You can find out how to invoke CMake on other SO questions.
You can get Makefiles and such to work on Windows, using projects like Cygwin and MSYS. I'd generally recommend you avoid that unless you really need it.
I'm not sure about my last step. I'm pretty sure if I did the installation as proposed in the link(and as required by fcl), I would have a new folder for the library including just an include folder and a lib folder. Now I'm just searching for the correct include files and the lib file in subfolders of subfolders hoping I include the right ones. I'm a bit lost.
This is where no "general" advice exists. Different authors put their output in different places. If their instructions didn't include this information, you're already doing the right thing. If you get really stuck, you can always just ask them.
I'd really like some general steps(if there are) to installing a library.
No such thing exists, but where these industry-standard tools are involved, you can usually go on general documentation for those tools, or from memory.
I also suggest you shop around for a general book on programming, as general principles should be covered in any good one of those.

How to use cmake to get rapidcheck (property based testing) working in C++?

I'd like to do some property-based testing in a C++ library I'm working on, and was thinking of going with RapidCheck unless somebody has a better idea. (I will need, for example, to generate arbitrary std::set<int>, and if I can place bounds on the range of int in the sets and the size of the sets, all the better.)
All this being said, I'm still a bit of a cmake newb. There appear to be no instructions in RapidCheck except to include it as part of the source code (although downloading it would be better). I have gotten to the point where I can include the headers for RapidCheck in my code, but when I try to build any app using RapidCheck, I'm told that there are symbols from RapidCheck missing or that the rapidcheck library is missing.
I'm assuming that I have to build RapidCheck itself as part of the project to generate the library, but I'm not entirely sure how to do this and it seems difficult to find any examples where this is done.
Does anyone have any suggestions of examples where such things are done so that I can see the string of commands necessary to build a 3rd party API and include the library when building the executables, or - even better - an example of a project using RapidCheck that does exactly this? The lack of documentation on how to set this up is discouraging.
I hope this is not overly vague. To summarize, what I'd like to do from cmake:
Preferably download RapidTest (although including the files directly from the RapidTest project would be fine as well).
Run the required commands and set up the necessary variables to have my test code (in ${PROJECT_SOURCE_DIR}/test) be able to access RapidTest headers.
Generate (if necessary) the RapidTest library and make it so that I can link it to the tests I'm running.
Thanks in advance for any help you might be able to offer!
This is probably not the right way to do this, but maybe it will help:
I was able to get this working by doing the following:
# from within the root of the rapidcheck repo:
$ cmake -DBUILD_SHARED_LIBS=true -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .
# Leave off the BUILD_SHARED_LIBS flag if you don't need an SO.
$ make
That built: librapidcheck.so and librapidcheck.a, which you can then copy / install as needed.
You'll also need the include directory with the headers for rapidcheck, but that's just in the source tree.
Add the include path to your compile commands using whatever build tool you want, and link with the compiled libraries (the .so and .a)

Add external library into current c++ project

I'm trying to use the xgboost library to basically translate a model I developed in R to C++.
I've not used an external library and other sources online did not help me much so decided to ask the question here to hopefully find the way to do it.
Here's the library I need to use in my C++ code: https://github.com/dmlc/xgboost
I'm using Visual Studio Ultimate 2013. I have a make file that I use to define my project.
I am looking for the proper way to use this library in my C++ code.
If you have ever used this library before, I really appreciate your help. Any hint will be appreciated.
To use the library, one needs to first build it. I could not manage to build the master branch. So, I decided to use the V0.4 instead. I built the solution in that branch using Visual Studio and it produced the dll and lib files. One can then import the .dll file by using Add_library to add it via Cmake. After Add_library, the include directories need to be added by include_directories command.
I need to generate .a and .so files in the end. I'm working in windows and could not find a way to generate these files, yet!
So I found the way to resolve this issue. I shared it in another answer
Add an external library (e.g., Eigen) via CMake
Hope this helps people who are looking!

SuperLU library XCode issues

I'm a newbie when compiling and adding 3rd party C++ libraries into XCode... So here's the problem. I have .cpp files wich depend on 3 libraries. OpenCV-Eigen-SuperLU. So far think that I managed to add correctly OpenCV-Eigen, but I got trouble adding SuperLU.
I'm really confused on where should I include source files paths??. Some tutorials say that I need to create a static lib... other that I need a framework... on the other hand there are XCode project template which just include src files into a folder inside XCode project's and then reference them from build setting properties. Anyway I really need some guidance here.
So here's my setup for most of the libs. I have a folder within the XCode project with all dependencies.
Then... on project build settings I setup header search paths-- and library search paths. To point that folder
Headers for SuperLU are found correctly... but I get Linker errors.
So as you can see, I have no idea what to do... I'm Sorry but as I told on beginning I'm a newbie adding libs into XCode... plus documentation of procedures it's not good at all. So if anybody could help me with some guidance or links where I can start understanding how the linking process works would be great.
I'm a little desperate at this point, os any kind of help would be really really great.
Thanks a lot.
The problem is that both SuperLU and Objective-C define YES and NO. So you get a conflict.
In the end I was advised to create a different target to compile all SuperLU as a pure c++ project with gcc compiler. That made it possible for iOS app to use the lib as a dependency.