How do I link yaml-cpp in my CMakelists.txt? - c++

So I currently have a project structure that looks like this and there is a cmakelists.txt in every directory:
/app
/source
/myApp
/<file where I want to use yaml-cpp/yaml.h>
/lib
/yaml-parser(yaml-cpp)
When I run out of source builds without including any mentions to yaml-cpp, it compiles the whole directory no problem(the examples of the yaml-cpp provided are compiled). However, when I try to add "yaml-cpp/yaml.h" the compiler says there's no such file or directory. I'm wondering where and how I link my project and yaml-cpp. I currently have not made any changes to the yaml-cpp cmakelists.txt https://github.com/jbeder/yaml-cpp

You ought to add_subdirectory to your root CMakeLists.txt and build yaml-cpp target. Then you'll be able to link w/ it. Just add yaml-cpp to the list of target_link_libraries of your binary.

Related

CMake output to build directory [duplicate]

Is it possible to specify build directory within CMakeLists file? If yes, how.
My aim is to be able to call "cmake" within top level source directory and have cmake figure out the build directory.
Afaik, with CMake the build directory is always the directory from where you invoke the cmake or ccmake command. So if you want to change the build directory, you have to change directories before running CMake.
To control the location where executables, static and shared libraries are placed once finished, you can modifiy CMAKE_RUNTIME_OUTPUT_DIRECTORY, CMAKE_ARCHIVE_OUTPUT_DIRECTORY, and CMAKE_LIBRARY_OUTPUT_DIRECTORY respectively.
By design, there is not a way to specify that in CMakeLists.txt. It is designed for the user to be able to build the project in whatever directory they want. The typical workflow is:
Check out the project source code.
Go to desired build directory, or the source dir if you plan to do an in-source build.
Run cmake or ccmake to configure the project in that build directory.
Build your project.
All of the directories specified within your CMakeLists.txt should be relative to the ${PROJECT_BINARY_DIR} and ${PROJECT_SOURCE_DIR} variables. In this way, your code becomes buildable across different platforms, which is the goal of CMake.

CMAKE How to set the working directory of an executable when generating for an IDE

For a project I build with Cmake I need to set the working directory of the final executable when generating for an IDE. Currently I am trying to do this for Xcode but I welcome anybody who know how to do the same for Visual Studio.
I have this hierarchy:
CMakeLists.txt
|
|- lib
|- CMakeLists.txt
-- ...
|- exec (use the lib created in the lib folder)
|-CMakeLists.txt
...
The executable is created in exec and uses the library compiled in lib.
For now I am using Xcode as the generator for Cmake. I need a way to give Xcode a working directory that is not the default one when executing the executable created at exec. To achieve that, I tried to set the property CMAKE_XCODE_SCHEME_WORKING_DIRECTORY to the path I need but it doesn't work. No matter in which CMakeLists.txt file I set the property, the working directory I specified is never taken into account.
I tried to set the property at the top of the topmost CMakeLists, or before the add_executable of the CMakeLists inside the exec folder but it doesn't change anything, the Working directory is not the one I specified. When I go to the settings of the scheme, the working directory is still the default one.
I tried this:
Topmost CMakeLists.txt:
project(...)
set(CMAKE_XCODE_SCHEME_WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/exec/test)
#...
add_subdirectory(exec)
add_subdirectory(lib)
CMakeLists.txt inside exec:
project(...)
set(CMAKE_XCODE_SCHEME_WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/test)
#...
add_executable(...)
#...
My question is : In the case of Xcode : Why setting CMAKE_XCODE_SCHEME_WORKING_DIRECTORY doesn't do anything ? When reading the doc, I thought it solved exactly my problem. And if somebody knows how to do the same thing for Visual Studio I'm interested

Installing FTP Client (Library) in C++ Ubuntu

I have found this library https://github.com/embeddedmz/ftpclient-cpp on GitHub but how to install it on Linux(Ubuntu) is quite obscure.
You will need CMake to generate a makefile for the static library or
to build the tests/code coverage program. Also make sure you have
libcurl and Google Test installed.
You can follow this script
https://gist.github.com/fideloper/f72997d2e2c9fbe66459 to install
libcurl.
This tutorial will help you installing properly Google Test on Ubuntu:
https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
The CMake script located in the tree will produce Makefiles for the
creation of the static library and for the unit tests program.
To create a debug static library and a test binary, change directory
to the one containing the first CMakeLists.txt and :
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE:STRING=Debug
make
It is not clear to me what "to the one containing the first CMakeLists.txt" refers to. Is it the one in the Gtest? The one in Curl? Or What?
After trying both (the Cmake in Gtest and Curl) I still get the error: "No such file or directory" while trying to #include "FTPClient.h" .
UPDATE:
Listing what I am doing:
I did git clone https://github.com/embeddedmz/ftpclient-cpp.git then made the build folder, navigate into it, I tried cmake .. -DCMAKE_BUILD_TYPE:STRING=Debug (this is the literal command I inserted) and I get
Cmake Error at CmakeLists.txt: 27 (add_subdirectory): add_subdirectory given source "TestFTP" which is not an existing directory
So what is wrong so far?
After you build the library, there will be a libftpclient.a generated in your build tree.
You can install it to your system as follows:
In this case, copy libftpclient.a to /usr/local/lib and the two header files in FTP to /usr/local/include.
You should then be able to include the header files by adding the -I/usr/local/include flag and link by adding -L/usr/local/lib -lftpclient.

CLion indexer does not resolve some includes in the project directory

I have a CLion C++ project that has the following structure:
project
---->my_includes
| ----> my_own.hpp
---->source
----> my_app
----> my_src.cpp
The first line of my_src.cpp is
#include "my_includes/my_own.hpp"
I use an external build system that requires this inclusion format. The problem is if I use a function in my source file defined in the included header, CLion says "Cannot find my_own.hpp" if I try to hover over the inclusion.
I tried marking the include directory as containing Project Source or Headers but this didn't fix it. Any ideas?
You need to create a CMakeLists.txt for CLion to be happy. It is enough to declare all the source files, you don't have to convert your scons (or any other build system) to cmake.
You don't even have to write the CMakeLists.txt by hand, you can ask CLion to do it:
File | New CMake Project from Sources... (since CLion 2019.2)
File | Import project ... | (older CLion)
and then point at the directory containing your project.
Now edit the generated CMakeLists.txt and add a cmake command to tell CLion where to find the includes (actually to tell the compiler, and CLion will reuse that information).
Since your source files use the include as #include "my_includes/my_own.hpp", you need to tell cmake the base directory containing directory my_includes:
include_directories(.)
Where the dot means the same directory as the one containing the CMakeLists.txt.
I tested with a project reproducing your layout and from my_src.cpp I can navigate to my_own.hpp.
Then to build you still have to use scons in a console. It is also possible to add a cmake command, add_custom_target() that will call your scons (or your make, or whatever), so that you can also navigate from CLion to the build errors.
This should be a CMake-based project to open correctly in CLion.
Check CMake basics tutorial if you are new to CMake: https://www.jetbrains.com/help/clion/2016.1/quick-cmake-tutorial.html
And for MakeFile + gcc (g++) projects, you can add the flag -I /Dir/To/Your/Project.
If CLion still shows errors with #include after recompiling the make file, delete the .idea folder and restart CLion.

Cmake generated Eclipse Project - No Source

I am attempting to use CMake to generate eclipse project files. Although I am able to build successfully, I am unable to browse or edit the source in eclipse. This is an out of tree build, where my build directory is at the same level as my source directory. I am using the following command to generate the eclipse files:
cmake -G"Eclipse CDT4 - Unix Makefiles" -DBoost_NO_BOOST_CMAKE=ON -DBOOST_ROOT=/usr/local/lib -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE ../proc
The source directory has many different modules, but the top level CMakeLists file is essentially a bunch of add_subdirectory commands adding each module. Is this incorrect?
I create the eclipse project with the following command
cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE -DCMAKE_BUILD_TYPE=Debug $PROJECT_SRC_DIR
// $PROJECT_SRC_DIR is where my highest level CMakeLists.txt is
My CMakeLists.txt does basically look like this
cmake_minimum_required (VERSION 3.5.0)
cmake_policy(VERSION 3.5.0)
project(myproject VERSION 0.0.0)
# definition, find packages, compiler settings
add_subdirectory(src) # where my code is
# src contains further subdirectories with CMakeLists.txt and subproject definition therein.
This creates an eclipse project with a virtual directory [Source Directory] which points to the directory where Eclipse shows the content of the highest level CMakeLists.txt file with a project(...) command. Furthermore it shows [Subprojects] and [Targets] where the sources are shown for cmake subprojects (the directory content from the level of CMakeLists.txt where the subproject is defined) or targets (the list of file which are combined to an executable with the add_executable() command.
Do you have a project defined?
I've made the same experience, both for Eclipse and KDevelop. CMake prepares the configuration such that you can compile and debug from within those IDEs, but it doesn't actually add any information that would help the IDE to list source files for browsing them.