Using cmake to include my source files to another existing project - c++

I'm trying to extend an open source project with my own source files, so that the executable being produced includes my code as well. Probably it's very easy, but somehow I'm overlooking it.
The build process is using cmake and the open source project contains a well written CMakeLists.txt. Instead of adding my source files in the directories of the open source project (and possibly changing CMakeLists.txt of the open source project), I prefer to separate my code in its own directory as follows:
src/opensourceproject/src/...
src/opensourceproject/include/...
...
src/myaddition/include/...
src/myaddition/src/...
Is there a way to wrap the build process and create a CMakeLists.txt that builds opensourceproject with my sources added to the opensource build? The reason why I want it, is that I'm more or less writing a plugin and I want my code to be present in the produced executable.

Related

using cmake, build a visual studio project with dependencies, being able to debug also into the dependencies

I have a C++ project source code with several dependencies (C++ packages, compiled libs and source).
I need to create a CMake file which will generate the Visual Studio solution and projects files in such a way that if I put a break point in one of the dependencies source code, the execution of the main project in debug mode would break and debug at that break point in the dependency.
Any suggestion is highly appreciated.
[EDIT]
If I use the packages, with the provided ...config.cmake files, the source files are not included in the generated project.
If I generate the MS projects individually and use include_external_msproject to put them together in a solution, I have the source code, but it is executed from somewhere else (binaries), so my breakpoints are ignored.
On the other hand, there is overlap from duplicated targets, like DOCUMENTATION for example, which come from each dependency, if I want to use add_subdirectory to add the deps to the main project
I ended up modifying the dependencies so that they implement unique target names for documentation, but if built individually to create the original named target: DOCUMENTATION.
doxygen_add_docs(DOCUMENTATION_${PROJECT_NAME}
doc
src/component
)
if (NOT (TARGET DOCUMENTATION))
add_custom_target(DOCUMENTATION COMMENT "workaround for multi-dependencies project")
add_dependencies(DOCUMENTATION DOCUMENTATION_${PROJECT_NAME})
endif()
You need to write a CMakeLists.txt with the appropriate compile options. To do that, you can set the following CMake values in the CMakeLists.txt file :
set(CMAKE_CXX_FLAGS "your-msvc-debug-options") or set(CMAKE_CXX_DEBUG_FLAGS "your-msvc-debug-options"). You may also use add_compile_options() which will set values for all your project (dependencies included if you build them with CMake)

Adding multiple source files to build in Eclipse

I'm developing a code which uses Easylogging++ as the underlying logging library. Recently, I wanted to update the library since it has some high visibility / high impact bugs and I found out that the library is divided into two files (.cc and .h). This new structure needs inclusion of the .cc file in the build string alongside the main program code.
I'm using Eclipse to develop the project and generate the make files to build the project. I need to tell Eclipse (Oxygen.1) that it needs to compile the .cc file alongside the main file while building the project, however I was unable to do so. Any help is greatly appreciated.
It's easier than I thought. Eclipse's managed build is more intelligent than it seems. Adding many source files under the /src folder causes Eclipse to automatically compile all the files under that folder, unless you exclude them.
This means adding the .h to /lib folder and the .cc file to the /src folder and modifying to .cc to look for the .h file under /lib have solved the problem neatly.
To complete the compilation I had to add some flags, since the developer likes to extensively modify his library between releases.
Everything is working fine again.

How to include a library in my code using cmake?

I have tried to ´make´ the library yaml-cpp, not sure I did it right, but how do I build it?
In the tutorial (https://github.com/jbeder/yaml-cpp/blob/master/README.md) it says to run cmake in the build dir, but cmake could not find the cmakelist file, so I did it in the source dir, but then what? How do I build it?
If someone could make a newbie step by step to get the library (or any library really) so I can include it in my code, that would be awesome.
Im using Windows 7, and compiling using terminal (using Codeblocks MinGW gcc/g++) and sublime text 3 editor.
Edit: I have not "make". How can I get this?
Here is the step by step guide:
For the purpose of this answer I will use cmake gui instead to highlight a few key points.
go to https://github.com/jbeder/yaml-cpp and download the root library.
open cmake gui and select the source directory as <my project>/yaml-cpp-master
select a directory for the build. I would call it <my project>/yaml-cpp-master/codeblocks_build
press configure and then check all the values.
press generate and wait for it to complete.
Find the generated codeblocks project file within <my project>/yaml-cpp-master/codeblocks_build
Compile the project as you normally would.
find the generated DLL files and link them to your project.
The reason why you are getting this error is because cmake is trying to find the source code in the directory build which is newly created as seen in the tutorial:
mkdir build
cd build
This is meant to specify to cmake where to build it in rather than where to build from. If you wish to use it via a command line you will need to tell cmake where to build and where the source is.
To then call the functions from that library you will need to link the header files (files that start with .h or .hpp) and the DLL libraries.
the .cpp .c etc is where the implementation is but .h .hpp is where the definitions are.
So when you are including like this: #include<something.h> you are including definitions which are later filled by the .cpp files however in case of a library they are instead filled from .dll or .o

Is there a way to add include directory from C++ code?

I have a project whose vcxproj file is auto generated prior of compilation by using a script during the build process.
this project dependes on a boost library which is installed in a known location on the build machine.
the project header file declares:
#include "boost/foreach.hpp"
which forces me to manually add the path to the boost root folder to the Additional Include Directories field in the vcxproj file.
As the project file is auto-generated it forces me to split the build process into two stages and edit the project file in between.
i have also tried to change the source file and add the full path in the #include statement:
#include "<path to boost root>/boost/foreach.hpp"
but then some boost internal include fails. Which means i can't proceed in this way.
I have read through Set #include directory from C++ code file to find there is no option to add the path from code.
As I'm now on VS2012/C++11 environment i wonder if anything changed in VS2012 / C++11?
Is it still impossible to add an Include directory using a code statement?
You mean dynamically? No, there is no way. The reason is simple:
When you are running your program, it's already compiled, hence the compiler has to know about all the files to include at compile time.
It seems you're using CMake. If that's the case, I recommend you to add the include dirs in the CMake file.
Firstly, doing this:
#include "<path to boost root>/boost/foreach.hpp"
should be strongly discouraged. By doing that, you're making your source code build-able only on your environment - as your project grows, it will be a nightmare to change the path, or for other developers to build it. And, as you discovered, you'll break any headers which are included further down the chain, which are using relative paths.
What kind of script is generating your project? Is it a custom one, or is it a well known build tool such as SCons or CMake? The correct solution is to fix your build script so that it generates the project with the additional include paths correctly.

CMake and Visual Studio resource files

I am converting a C++ project created using Visual Studio 2005 to CMake and have stumbled upon a bit of a problem with resource files that are included in the project.
The project includes a .rc file, a bunch of .ico files and a .rc2 file.
The regular .rc file works fine in the generated project and uses the resource compiler. The .ico and .rc2 files however are causing problems when they are just being included, because in the generated project Visual Studio attempts to compile them using the C/C++ compiler.
I assume that these files are included by the .rc file, so it would probably work to just not include them in the CMakeLists.txt file, but since it is obviously possible to list them in the project (they are visible in the original project) I would like to do so, so that the user of the generated project can see that these files are being used.
What is the correct way to handle these extra VS resource files in CMake?
Try to set_source_files_properties(your.ico your.rc2 PROPERTIES LANGUAGE RC).
By default it shouldn't do anything with those files. The source file property LANGUAGE should be empty and thus the action for the file should be checked by the file type. Which shouldn't be anything since it's not something it should compile.
Check your CMakeLists.txt that is doesn't contain a set_source_files_properties command that would mess with that property.
If you want to do something with the files, here are two ways to do things:
With add_custom_target you can add them and run custom commands for them when you build the project. Granted that the files have changed.
With configure_file you can easily copy them to a build directory if needed. With the COPYONLY flag.