How to add files to Eclipse CDT project with CMake? - c++

I'm having problem getting the source and header files added into my Eclipse CDT project with CMake. In my test project (which generates and builds fine) I have the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(WINCA)
file(GLOB WINCA_SRC_BASE "${WINCA_SOURCE_DIR}/src/*.cpp")
file(GLOB WINCA_SRC_HPP_BASE "${WINCA_SOURCE_DIR}/inc/*.hpp")
add_library(WINCABase ${WINCA_SRC_BASE} ${WINCA_SRC_HPP_BASE})
This works fine but the resulting Eclipse project files contains no links to the source or header files. Anyone knows why? Are there any other cmake command I have to use to actually add the files into the project?

I realize it's been a while since you've post this, but fwiw, it work's for me fine with CMake 2.6 or 2.7 (trunk) versions, generating for Eclipse/Ganymede. What I do is first run
cmake -G "Eclipse CDT4 - Unix Makefiles" /path/to/src
which generates the Eclipse project files as well as the makefiles, then "Import Project" in Eclipse.
Works beautifully...
sly

I use CMake 2.4, not 2.6 but in 2.4 they specifically warn against using GLOBs to find the files to build.
This is because it will notice if new files are added or deleted, so it will not be able to figure out the dependencies.
If you have to explicitly add the files to your CMakeLists.txt then this file will be newer than the makefiles and the cache files. So CMake will know to regenerate them.
If the files are added with a glob no files CMake knows about change with you add new files so CMake doesn't know that it has to regenerate the makefiles etc. This is the same for regular makefiles and Visual Studio projects.
Unless the CMake 2.6 docs explicitly says it is ok to add files like this I would avoid it. It is not that hard to manage the source files in cmake. How often do you add new files?

The problem I had was I made an "in-source" build instead of an "out-of-source" build. Now it works fine, and it was actually lots of info on this on the Wiki but somehow I misunderstood it.

Related

C++, setting up include with Cmake

I want to use the ICU package and I tried installing with and without vcpkg, and with vcpkg I do not get a directory for where it was installed like I see in tutorials, so I had to manually find it nor did I get a Cmake toolchain paste line from installing with vcpkg. As for the Cmake file, I try following this tutorial
https://www.youtube.com/watch?v=FeBzSYiWkEU
but when I go to the configuration manager in Visual Studio I am brought to a json file with no option to change the toolchain, and when I try to go to the settings json file in Visual Studio Code instead, I instead find three different settings json files, none of which have the toolchain file option to change like with Visual Studio. I have had similar difficulties with the Cmake file when trying to install other packages for C++ and have no idea what I am missing from tutorials that seems to no matter what have the Cmakefile.txt never work for me.
I would like to just be able to use the ICU package without having to use Visual Studio and just be able to statically link the package with my .cpp file in the same folder that VS Code uses, and just g++ compile from cmd, however I am in over my head every time I try to get the CMake file to work. If all I know for sure is that I have the latest ICU package downloaded with vcpkg, how would I go about setting up the CMake file?
cmake_minimum_required(VERSION 3.24)
project(MyICUProject)
find_package(ICU REQUIRED)
add_library(MyLibUsingICU lib.cpp)
target_link_libraries(MyLibUsingICU PRIVATE ICU::i18n) # or whatever you use from icu. check https://cmake.org/cmake/help/latest/module/FindICU.html
have a manifest (vcpkg.json) along the sources
{
"name": "MyICUProject",
"description": "<description>",
"dependencies": ["icu"]
}
Invoke with (if you are in the source dir):
cmake -G "Ninja" -B "build" -S . -DCMAKE_TOOLCHAIN_FILE=<path_to_vcpkg_toolchain>/vcpkg.cmake

Compiling and Linking to Visual Studio 2022 using OpenCV source code built as Win32 from CMake C++

I'm trying to use OpenCV with Dear ImGui in Visual Studio 2022. I'm new to C/C++ libraries and building in general, so I'm unsure if I'm doing anything right. ImGui uses 32-bit architecture and I've used Cmake gui to compile the source code as Win32. I think I have the compiled source code, but it seems to be different than downloading the pre-built libraries. File Explorer Screenshot. I've added the bin to PATH environmental variable, and in Visual Studio tried adding \include to Include Directories, \lib or \lib\Debug to Library Directories, and opencv_world460d.lib to Additional Dependencies. The program still runs, but it doesn't seem to include anything related to OpenCV in the #include files. I found a few .dll files in bin\Debug, but I'm not sure if I should bother with that. I think I could move the source code into the project, but I'm fairly certain that isn't the proper way to do it. Any help would be appreciated.
I needed to run the install target:
You may have built the project, but probably you didn't run the install target. Try running cmake --build <build_dir> --config Release and then cmake --install <build_dir> --config Release, where <build_dir> is a placeholder for the path to the build dir shown in the screenshot. The latter command probably requires admin privileges. Probably best to check the docs of the lib, if there's a step by step instruction for building & installing the whole thing. –
fabian

Maintaining Makefiles and CMakeLists.txt

When you're working to very big projects with a large amount of people, maintaining both Makefiles and CMakeLists.txt could be very difficult. I know that CMakeLists.txt could generate Makefiles, but suppose that I want to compile my code by using make and in the same time, use an IDE to have a kind of intellisense. How can I achieve it?
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
CMake is not a build manager!
It is a generator of files for other build managers!
So you can generate project for IDEs (Visual Studio, Xcode, CodeBlocks, ..) or generate MakeFile, or whatever you prefer and CMake supports. So I do not understand why do you maintain CMakeLists.txt and Makefiles at the same time.
Just maintain CMakeLists.txt and then generate respective Makefiles from it.
For details see CMake Documentation - generators:
Command-Line Build Tool Generators
These generators support command-line build tools. In order to use them, one must launch CMake from a command-line prompt whose environment is already configured for the chosen compiler and build tool.
Borland Makefiles
MSYS Makefiles
MinGW Makefiles
NMake Makefiles
NMake Makefiles JOM
Ninja
Unix Makefiles
Watcom WMake
Bottom line you should be able to generate files for IDE or Makefile for you favorite build manager without any complications.
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
WAT? You are doing something very strange and most probably wrong and you didn't provide any details abut that.
Concerning intellisense I would say:
Visual Studio 2017 as built-in support for CMake, see announcement
CMake can generate a json file with all commands see CMAKE_EXPORT_COMPILE_COMMANDS
so you can use it to have smart clang based completion in vim
QtCreator as built-in support for CMake so you'll have completion too.
Extra tip at first: Do not try to mix both handmade make files and CMake over the whole time of the project.
The normal way is to create the make files using cmake. The extra of cmake is the cross platform and in your case the nativ support by some IDEs, eg. QtCreator, CLion(awaik). For a wide range of other IDE cmake can generate the project files.
Example Workflow using QtCreator:
Create a simple CMakeLists.txt, at least with the name of the project
Load this into QtCreator
Add source files, update the CMakeLists.txt from within QtCreator
Build from within QtCreator, repeat
But the same CMakeLists.txt will be used to create the make file on command line. Or on your build server or CI system.

Generate separate eclipse projects using CMake

I'm using CMake to generate a Eclipse CDT project. This "project" really contains a ton of sub-projects in our repo. This is because I'm defining
project (sample_project)
in my CMakeLists.txt files and using
add_subdirectory(sample_project_directory)
in my parent directory to chain all my CMakeLists.txt together. When I run cmake -G "Eclipse CDT4 - Unix Makefiles" ..\src it will actually generate just one .project and .cproject file and uses virtual folders to add all the sub-projects. I would much rather generate a separate project file per project. This way I can build each project individually rather than everything in one go. Is this possible? When I generate it for Visual Studio it will generate a master Solution file with a bunch of Projects for every project (sample_project) I've defined. This is much more ideal.

Eclipse doesn't see include files when checking syntax but does when building or running the application

I started with building my project with CMake by using the cmake <folder> -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug command (I'm using Ubuntu 14.04). Say the results were written to /home/student/tempFolder/HelloFrameworkApp folder.
The thing is that I have header files needed for he application in /home/student/git/fagot/sw/branches/head/framework/include folder. This is also described in CMakeLists so after I import my project from /home/student/tempFolder/HelloFrameworkApp to Eclipse as existing code as makefile project, it can successfully be built and runs just as expected.
But in the code edit area there are tons of red underlining which makes it clear that Eclipse can't see the include folder I mentioned before. I tried going to Properties -> C/C++ General -> Paths and symbols and adding that include folder to all configurations and languages but it didn't help.
Any suggestions?
CMake creates an Eclipse project for you. Therefore you need to use: Import->General->Existing Project into Workspace.