Cmake generated Eclipse Project - No Source - c++

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.

Related

Set cache, build files and binary files directories for Visual Studio in CMake

Current environment
I have the following file structure:
project_root/
__main.cpp # app starting point
__CMakeLists.txt # CMake build starting point
__include/ # header files
__src/ # source files
__lib/ # libraries/dependencies which are also CMake projects
__res/
__build/ # CMake cache, build files
__bin/ # compiled binaries
I've been working with VS Code for both Windows and Linux platforms, but I would like to try out Visual Studio environment.
So far, using CMake Tools extension worked pretty well placing build files in build/ and binaries in /bin with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
project(OPENGL_TEMPLATE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/build")
# targets, libs and properties setup
The problem
Unfortunately, Visual Studio overrides these variables and creates some project_root/out/Debug/ directory, which does not fit my current directory tree.
There are two ways (to my knowledge) to run CMake project in Visual Studio, which I would like both to be compatible with my current directory tree:
Generate VS solution with CMake from terminal or CMake GUI and open it;
Open directory from File Explorer with "Open with Visual Studio"
So far, I have come up with this CMakeLists.txt:
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build")
set(CMAKE_BINARY_DIR "${CMAKE_SOURCE_DIR}/build")
project(PROJECT_NAME)
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds are forbidden.")
endif()
add_executable(main)
set_target_properties(main PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin"
ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/build"
VS_STARTUP_PROJECT main
FOLDER "App"
)
# other targets, libs and properties setup
And it works when generating .sln solution with CMake (but does not set the VS_STARTUP_PROJECT for some reason, but this is for another SO question), here build files go to build/ and binaries to bin/.
When using the second approach, VS still generates everything into out/Debug/, seemingly completely ignoring CMAKE_BINARY_DIR (which they claim corresponds to buildRoot VS configuration variable) and ARCHIVE_OUTPUT_DIRECTORY, RUNTIME_OUTPUT_DIRECTORY target properties.
The question (finally)
How can I setup CMake variables so that Visual Studio respects them (and does not override) using both approaches? Ideal situation would be to just open a clean project and be ready to configure, build and run, which means no additional tinkering in Visual Studio configuration files/settings. I am using Visual Studio 2022 Community 17.3.3.

Binaries missing after successful build?

I have a CMake project that I use to generate a Visual Studio solution, which I then try to compile. For some reason, the library file after compilation is nonexistent. I've searched my entire project folder, and cannot find any library files. Here's my CMake setup:
Project root:
cmake_minimum_required(VERSION 3.22)
project(ShadowContainers)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
include_directories(include/)
add_subdirectory(library)
...
library subdirectory:
add_library(libShadows shadows.tpp ../include/shadows.hpp)
set_target_properties(libShadows PROPERTIES LINKER_LANGUAGE CXX) # a tpp and an hpp aren't enough to make the system certain it's c++
target_compile_features(libShadows PRIVATE cxx_std_23)
The MSbuild output contains these lines:
Done Building Project "C:\Users\[..]\projects\Shadow Array\library\libShadows.vcxproj" (default targets).
Done Building Project "C:\Users\[..]\projects\Shadow Array\library\libShadows.vcxproj.metaproj" (default targets).
Even if libShadows is the only target in my project, it's nowhere in my project directory.
Has anybody else had this experience? Any help is appreciated. Thanks!
Edit:
To compile the project, I have tried:
My typical process (working directory = the project root):
cmake .
msbuild ShadowContainers.sln
An alternate process (working directory also project root):
mkdir build
cd build
cmake ..
cmake --build .
Both have produced the same result, albeit with VS and CMake files in different places. No library is outputted either way.
I was able to find the issue. CMake (and conventions for that matter) do not see a .tpp as a code file (similarly to how they treat header files). Thus, I was trying to compile a target with no legitimate code files. Template-only libraries are not libraries at all, so I should not be attempting to compile this standalone, and should instead put the .hpp and .tpp as the include in my projects!

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.

How to configure cmake-based project for a build with Qt

here is a doc about that, but it doesn't look correct for me ,
so I've copied cmake instructions into my cmakelists.txt and it doesn't work.
it's clear why it doesn't work - because there is no one instruction how to search qt:
I suppose two cases:
some additional cmake instructions requires to set
some environment variables should be set
but nothing about that.
the instruction :
find_package(Qt5Widgets)
refers to extra cmake script from qt kit , isn't it?
I see the directory with that name (annd contains *.cmake scripts but another names) but there is no the script with this name
these are the only .cmake files with qt in names in cmake 3.6 :
cmake-3.6\Modules\DeployQt4.cmake
cmake-3.6\Modules\FindosgQt.cmake
cmake-3.6\Modules\FindQt.cmake
cmake-3.6\Modules\FindQt3.cmake
cmake-3.6\Modules\FindQt4.cmake
cmake-3.6\Modules\Qt4ConfigDependentSettings.cmake
cmake-3.6\Modules\Qt4Macros.cmake
cmake-3.6\Modules\UseQt4.cmake
===================
C:\dev\tools\CLion.RC\bin\cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug
-G "CodeBlocks - MinGW Makefiles" C:\dev\workspace\algolist.v2 CMake Warning at CMakeLists.txt:14 (find_package): By not providing
"FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project has asked
CMake to find a package configuration file provided by "Qt5Widgets",
but CMake did not find one.
Could not find a package configuration file provided by "Qt5Widgets"
with any of the following names:
Qt5WidgetsConfig.cmake
qt5widgets-config.cmake
Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or
set "Qt5Widgets_DIR" to a directory containing one of the above
files. If "Qt5Widgets" provides a separate development package or
SDK, be sure it has been installed.
CMake Error at CMakeLists.txt:20 (target_link_libraries): Cannot
specify link libraries for target "helloworld" which is not built by
this project.
-- Configuring incomplete, errors occurred! See also "C:/dev/workspace/algolist.v2/cmake-build-debug/CMakeFiles/CMakeOutput.log".
seems it found Qt5Widgets, but absolutely not clear how it does this...
so it looks for cmake files inside Qt5Widgets folder, in Qt kit.
I can add this folder to the path but I don't think this is a valid way,
because there is a lot of subfolders with cmake files
Qt5.8 beta, built with MinGW, Win10, cmake 3.6
You need to use CMAKE_PREFIX_PATH.
For example:
cmake.exe -DCMAKE_PREFIX_PATH="C:/path/to/Qt/5.X/compiler/lib/cmake"

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

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.