I'm doing some experiments to learn CMake. So the commands stay in my mind. I created a project to test what I just learned. However, I have a problem.
The structure of my project is as follows:
├── bin
├── CMakeLists.txt
└── src
├── Configuration
│ ├── CMakeLists.txt
│ ├── Test
│ │ └── TestConfiguration.h
├── Array
│ └── Array.h
├── CMakeLists.txt
├── Test2
│ ├── CMakeLists.txt
│ ├── Test2.cpp
│ ├── Test2.h
│ └── Test2-1.h
├── Main
│ ├── CMakeLists.txt
│ ├── Config.h
│ └── Main.h
├── Test3
│ ├── CMakeLists.txt
│ ├── Time.h
│ ├── Timer
│ │ ├── CMakeLists.txt
│ │ ├── Iterate.h
│ │ ├── Run.h
│ │ ├── Serial.cmpl.cpp
│ │ └── Serial.h
│ ├── Smart.h
│ ├── Counting.h
│ ├── Mute.h
│ └── MainTest.h
└── Utilities
├── CMakeLists.txt
├── Inform.h
├── Settings.h
├── Print.h
└── Const.h
But I didn't understand how I should make these CMakeLists.txt files. For example, the file src/Utilities/Inform.h uses the following header:
// src/Utilities/Inform.h
#include "Main/Config.h"
I've tried everything I've seen on the internet and stackoverflow to edit the src/Utilities/CMakeLists.txt file. But no matter what I do, it never sees the Main/Config.h file. I just need to do something like ../../Main/Config.h.
The same problem applies to other folders. What I want to learn here is to be able to navigate and call all files in the project with CMakeLists.txt. While doing this, I tried many of the following parameters:
add_library
target_include_directories
target_link_libraries
link_directories
link_libraries
I think there's something I'm missing or misunderstood. I would be glad if you help me in this regard. If you tell me how to edit the src/Utilities/CMakeLists.txt file, I will try to fill the others accordingly.
Additionally, there is something I'm curious about. Do I also need to edit the src/CMakeLists.txt file? Or is it enough if I just edit for example src/Utilities/CMakeLists.txt?
Also, I don't know if it will be needed additionally, but I'm using cmake version 3.16.3. My development environment is an x86_64 20.04.1 Ubuntu-based Elementary OS.
I've read the official documentation for CMake 3.16 and the answers from fellow developers on StackOverFlow. I want to use the header file in the parent folder in a header in subdirectories. But many ways I've tried are wrong. There is always an error in the include path I entered. I want to learn from experienced developers what I did wrong.
Related
OpenCV is installed from the source on my Linux (Ubuntu 18.04.6 LTS) machine. The path is a bit different i.e. /usr/local/<blah_blah> and the directory tree looks somewhat like this:
milan#my_machine:/usr/local/<blah_blah>$ tree -L 4
.
├── bin
│ ├── opencv_annotation
│ └── ...
├── include
│ └── opencv4
│ └── opencv2
│ ├── ...
│ ├── core
│ ├── core.hpp
│ ├── ...
│ └── ...
├── lib
│ ├── cmake
│ │ └── opencv4
│ │ ├── OpenCVConfig.cmake
│ │ └── ...
│ ├── ...
│ ├── libopencv_core.so -> libopencv_core.so.4.2
│ ├── libopencv_core.so.4.2 -> libopencv_core.so.4.2.0
│ ├── libopencv_core.so.4.2.0
│ ├── ...
│ ├── ...
│ ├── opencv4
│ │ └── 3rdparty
│ │ ├── ...
│ │ └── ...
│ ├── python2.7
│ │ └── dist-packages
│ │ └── cv2
│ └── python3.6
│ └── dist-packages
│ └── cv2
└── share
├── licenses
│ └── opencv4
│ ├── ...
│ └── ...
└── opencv4
├── ...
│ └── ...
├── ...
└── ...
I had a similar issue for PCL (Point Cloud Library) in the past and my answer/solution fixed that. So, I tried something similar:
In settings.json, I put:
"C_Cpp.default.includePath": [
"/usr/local/<blah_blah>/include/opencv4/opencv2/**",
"/usr/local/<blah_blah>/include/opencv4/opencv2/core",
"/usr/local/<blah_blah>/include/opencv4/opencv2/core/*",
"/usr/local/<blah_blah>/include/opencv4/opencv2/core/**"
],
and in the c_cpp_properties.json file, I put:
"includePath": [
"${workspaceFolder}/**",
"${default}"
],
However, doing this is not fixing the issue. C++ IntelliSense/autocomplete still does not work for OpenCV C++. So, how to fix this issue?
Sample Code:
Note1:
In cmake, /usr/local/<blah_blah>/include/opencv4 is used under include_directories.
Compilation and execution work fine.
Note2: the following questions/issues are different from mine:
VSCode autocomplete not working for OpenCV installed from source -- for OpenCV Python, not C++
cv2 (opencv-python) intellisense not working -- for OpenCV Python, not C++
It turned out that in my settings.json file, the includePaths were set like this:
"C_Cpp.default.includePath": [
"/usr/local/<blah_blah>/include/opencv4/opencv2/**",
"/usr/local/<blah_blah>/include/opencv4/opencv2/core.hpp",
"/usr/local/<blah_blah>/include/opencv4/opencv2/core",
.
.
],
However, in my code, the headers were included like:
#include <opencv2/core.hpp>
If the opencv2 folder needs to be included in the #include directive, the includePaths should look like this:
"C_Cpp.default.includePath": [
"/usr/local/<blah_blah>/include/opencv4",
.
.
],
So, the following includePaths configuration fixed the issue with IntelliSense/autocompletion for OpenCV:
"C_Cpp.default.includePath": [
"/usr/local/<blah_blah>/include/opencv4",
"/usr/local/<blah_blah>/include/opencv4/**",
],
For a detailed explanation, take a look into the issue (Issue 9900) I created on vscode-cpptools GitHub page, particularly this thread/reply.
Special thanks to vscode-cpptools and vscode-cmake-tools team!
I am trying to run wxWidget example using cmake but unable to include the headers of wxWidgets in my C++ project(CMakeLists.txt). If i run the program using the command
g++ main.cpp `wx-config --cppflags --libs` -o wxTest
the program works and display the window. But how can i do that using CMakeLists.txt file. For example, usually i create a separate folder called external-libs and then inside it create a folder with the name whateverlibraryname and then inside it create a header and src and lib folder where i place the header files, any source files and .so files respectively. But in the case of wxWidgets i have several static library files and also inside the header there are many separate folders and i don't know how to include them all. They produce the error:
fatal error: wx/wx.h: No such file or directory
#include <wx/wx.h>
I am using Ubuntu 18.04 and my project directory structure is as follows:
├── build
└── source
├── CMakeLists.txt
├── external-libraries
│ └── wxWidgets
│ ├── headers
│ │ ├── msvc
│ │ │ └── wx
│ │ └── wx
│ │ ├── android
│ │ ├── aui
│ │ ├── dfb
│ │ ├── generic
│ │ ├── gtk
│ │ ├── gtk1
│ │ ├── html
│ │ ├── meta
│ │ ├── motif
│ │ ├── msw
│ │ ├── osx
│ │ ├── persist
│ │ ├── private
│ │ ├── propgrid
│ │ ├── protocol
│ │ ├── qt
│ │ ├── ribbon
│ │ ├── richtext
│ │ ├── stc
│ │ ├── univ
│ │ ├── unix
│ │ ├── x11
│ │ ├── xml
│ │ └── xrc
│ ├── lib
├── libwx_baseu-3.1.a
│ │ ├── libwx_baseu_net-3.1.a
│ │ ├── libwx_baseu_xml-3.1.a
│ │ ├── libwx_gtk3u_adv-3.1.a
│ │ ├── libwx_gtk3u_aui-3.1.a
│ │ ├── libwx_gtk3u_core-3.1.a
│ │ ├── libwx_gtk3u_gl-3.1.a
│ │ ├── libwx_gtk3u_html-3.1.a
│ │ ├── libwx_gtk3u_propgrid-3.1.a
│ │ ├── libwx_gtk3u_qa-3.1.a
│ │ ├── libwx_gtk3u_ribbon-3.1.a
│ │ ├── libwx_gtk3u_richtext-3.1.a
│ │ ├── libwx_gtk3u_stc-3.1.a
│ │ ├── libwx_gtk3u_xrc-3.1.a
│ │ ├── libwxjpeg-3.1.a
│ │ ├── libwxregexu-3.1.a
│ │ ├── libwxscintilla-3.1.a
│ │ └── libwxtiff-3.1.a
│ └── src
├── main.cpp
Main.cpp has #include<wx/wx.h> at the top. I am using VSCode and when i ran the program using g++(command described above) it works but doesn't workwhen i run the same program using CMake. That is how to include the header folder and use the lib folder that contains all the wxWidgets headers and library files. What should be the contents of the CMakeLists.txt files and what are the other necessary things that i have to do to make this work.
CMake has first-party support for wxWidgets, here's an example:
cmake_minimum_required(VERSION 3.20)
project(wxTest)
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
add_executable(wxTest main.cpp)
target_link_libraries(wxTest PRIVATE ${wxWidgets_LIBRARIES})
It's a bit unfortunate how legacy this module is. The above code would be much better off using imported targets and not need to do this weird dance with include(${wxWidgets_USE_FILE}), but alas. At least it's documented.
There are various variables you can set to help it find your wxWidgets installation. See the documentation for more details: https://cmake.org/cmake/help/latest/module/FindwxWidgets.html
I have been trying to figure out a good directory structure that is maintainable for large C++ projects. During my search I came across this resource here link. If I loosely follow the structure stated in that document, it seems I get something similar to the following
.
├── CMakeLists.txt
├── build
│ ├── Executable-OutputA
│ └── Library-OutputA
├── cmake
│ └── *.cmake
├── docs
├── include
│ └── *.h
├── lib
│ ├── LibraryA
│ │ ├── *.cpp
│ │ └── *.h
│ └── LibraryB
│ ├── *.cpp
│ └── *.h
├── src
│ ├── ExecutableA
│ │ ├── *.cpp
│ │ └── *.h
│ └── ExecutableB
│ ├── *.cpp
│ └── *.h
├── tests
└── third_party
├── External-ProjectA
└── External-ProjectB
build: holds the outputted executables and libraries generated by the project
cmake: holds all the cmake packages that the project may require
doc: holds documentation files, typically doxygen
include: holds public headers files (might not be needed, not sure)
lib: holds all the libraries the user creates with their respective source and header files
src: holds all the executable projects the user makes with their respective headers and source files
tests: files to test both the executables and libraries
third_party: any third party projects, libraries, ect. usually downloaded from online or cloned
I believe this is an appropriate structure for large projects, but I do not have too much experience with projects that produce more than 3 or 4 targets. I want to ask the community for feedback and if they agree with the structure laid out above, or have better suggestions.
Edit: I have not been able to find too many posts detailing multiple target outputs as well as third party dependencies for large projects.
I'm working on a project that encompasses an SDL-based game framework, an editor, and a game. The directory structure is shaping up to be something like this (edited for brevity)
├── CMake
├── Libraries
├── Resources
└── Source
├── Editor
├── Framework
│ └── Source
│ ├── AI
│ │ ├── Private
│ │ └── Public
│ ├── Game
│ │ ├── Private
│ │ └── Public
│ ├── Graphics
│ │ ├── Private
│ │ └── Public
│ └── Networking
├── Game
│ └── Source
│ └── Runtime
│ └── Launch
│ ├── Private
│ └── Public
└── Server
My add_executable command is being ran in Game/Source/Runtime/Launch/Private, and is dependent on files from the other modules.
According to some CMake how-tos and the accepted response for the following question, the headers for a given target should be included in the add_executable call in order for them to be listed as dependencies in the makefile.
How to properly add include directories with CMake
My question is, what is the cleanest way to accomplish this, given the abundance of header files and directories in my project? I can't imagine that the best practice would be to maintain a huge list of files directly in the add_executable call, but I could be wrong.
I was thinking that each directory's CMakeLists.txt could be responsible for adding to a variable that would eventually be used in add_executable, but distributing that intent through so many files seems like poor practice. Is there a better way?
You can follow exactly the pattern in the link you sent. For each library (I suppose they are libraries/targets in CMake), use:
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Private PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Public)
By doing this, you tell CMake that the Private folders are just for the current target, but if someone uses the target for another target, it should add the Public include directories.
Now, you just need to do:
target_link_libraries(${target} PRIVATE Framework_Source)
If Framework_Source is the name of a target. ${target} is the name of the target you are currently building.
Is there any guide how to start with HTMLBars? I am following "building HTMLBars" section but finally I am stuck. I have run building tool and now I have files in my dist directory like this:
.
├── htmlbars-compiler.amd.js
├── htmlbars-runtime.amd.js
├── morph.amd.js
├── test
│ ├── htmlbars-compiler-tests.amd.js
│ ├── htmlbars-runtime-tests.amd.js
│ ├── index.html
│ ├── loader.js
│ ├── morph-tests.amd.js
│ ├── packages-config.js
│ ├── qunit.css
│ └── qunit.js
└── vendor
├── handlebars.amd.js
└── simple-html-tokenizer.amd.js
Which should I add to my ember project and is that all or have I to do something more? Is this library ready or it is still unusable for ember?
Not even close to ready yet, I'd love to give more info, but there really isn't any. Last I heard they wanted it as a beta in 1.9, but we'll see.