Nested CmakeLists.txt seems to forget qt4 package - cmake

I've got a new project to work on and it's being built by cmake. I'm sort of familiar with the system but not really a user that knows what's really going on. The project works with qt4 and in a subdirectory of the root it uses the command "qt4_wrap_cpp(...)". The problem is that cmake throws an error : Unknown Cmake command "qt4_wrap_cpp". The CMakeLists.txt in the top-level folder has the line "find_package(qt4)" in it and all i've seen this far suggested adding that line.
Does anyone have an idea what i could do next to tell cmake i've already added qt4 ?
EDIT: Structure example
root //here lies the top-level cmakelists
->find_package(qt4 required)
->add_subdirectory(sub)
sub:
->add_subdirectory(another_sub)
another_sub:
->qt4_wrap_cpp()

Related

CMake add_subdirectory() for SDL_image out of tree build

I cloned the SDL_image library and wish to build it using a simple add_subdirectory(...) command. My current directory structure is as follows.
├───lib
│ ├───sdl_image
| ├─── ...
├───build
I have a CMakeLists.txt which is as follows.
cmake_minimum_required(VERSION 3.13)
project(test)
add_subdirectory(lib/sdl_image)
Upon execution (For my environment, cmake . -G "MinGW Makefiles"), I get the following error.
CMake Error at lib/sdl_image/CMakeLists.txt:18 (message):
Prevented in-tree built. Please create a build directory outside of the
SDL_image source code and call cmake from there
In an attempt to fix this, I modified my CMakeLists.txt add_subdirectory(...) command as follows. If I understand correctly, this should specify the output directory to build/sdl_image, outside of the SDL_image source code.
...
add_subdirectory(lib/sdl_image build/sdl_image)
However, I still get the same error. The line that is giving me the error under lib/sdl_image/CMakeLists.txt is as follows.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR ...)
endif()
I don't understand why this condition is getting triggered since I've specified the source_dir and binary_dir (parameters in add_subdirectory(...)) as very different paths. I also tried add_subdirectory(lib/sdl_image ../../build/sdl_image) in case it was treated as relative to the source_dir. This is still not working.
Any help is appreciated. Thanks.
This is about building in sources (calling cmake in your sources), not a path problem where to put sdl_image.
You probably call cmake from within your source directory which is considered a bad practice (same thing when using autotools, or any other build generator).
So you should have some kind of build tree like:
MyProjectWorkspace
|
\_ sources (tree in your case)
\_ build
and invoke cake with cmake ../build from the build directory.
The reason is that when building in sources, you somehow "pollute" your sources. Very likely you will need to add some .gitignore (if using git) and take special care not to commit thing that are built.
Moreover, when generating code, the generated code will appear in the source tree leading to some confusions at some point (you edit the generated file and see it deleted later).
It is also handy: to completely clear a build, you only need to remove the content of the build directory (would be much harder within the sources)
Last but not least, this also ease the packager's job as usually, the use off source builds.

CMake: target_include_directories and #include

I have the following layout. It is basically a certain class, and within its directory there is a subdirectory containing unit tests:
The class being tested is built like this:
add_library(experimental_run PUBLIC ${PROJECT_SOURCE_DIR}/experiments/experimental_run/experimental_run.cpp)
target_link_libraries(experimental_run PUBLIC nlohmann_json::nlohmann_json)
(this is the outermost CMakeLists in the image).
The tests-directory's CMakeLists.txt's contents are as follows:
cmake_minimum_required(VERSION 3.13)
add_executable(test_experimental_run_obj ${CMAKE_CURRENT_SOURCE_DIR}/test_experimental_run.cpp)
target_compile_options(test_experimental_run_obj PUBLIC -pg -g -O2)
target_link_libraries(test_experimental_run_obj PUBLIC experimental_run GTest::GTest GTest::Main)
target_include_directories(
test_experimental_run_obj PUBLIC
${PROJECT_SOURCE_DIR}/experiments/experimental_run/)
Essentially, first I am building the class as a library,
and then try to build the unit tests linking them towards
the library and including the appropriate directory
with target_include_directories and ${PROJECT_SOURCE_DIR}, where the latter
stores the root directory of the project.
Now this looks like I am effectively telling CMake "Feel free to look into the included
directories for anything you find in my #includes." However, my unit-test file
has it in red:
and prompts me to include the files using "../"
Now, why then I bothered to have the target_include_directories in the first place,
if I am to explicitly show the location anyway? Isn't the main idea
to make the whole thing flexible, without code duplication?
What am I missing?
EDIT: now that I went ahead and did include the "../"-prefix, the unit tests fail to compile, since the linked library experimental_run is non-existent. No wonder, since CLion does not seem to recognize it as something it needs to build.
EDIT: I've figured out one thing for now. CMAKE_PROJECT_SOURCE_DIR refers to the directory of where this _very CMakeLists resides, i.e. referring to the project's argument. This way, I can load the outermost library as a CMake project, but however cannot link against it inside the tests still.
(final) UPDATE:
Let's say I found a "workaround" this by adding
set(SOURCES ./test_experimental_run.cpp ../experimental_run.cpp)
set(TARGET test_experimental_run_obj)
into the CMakeLists of the unit-tests directory, and to no surprise it
compiles and works fine. If I remove he second *.cpp file, however,
and add experimental_run to target_link_libraries, I get
[ 50%] Linking CXX executable test_experimental_run_obj
/usr/bin/ld: cannot find -lexperimental_run
collect2: error: ld returned 1 exit status
Essentially the question is how to test the library assuming it is compiled,
and without having to specify its source code inside the unit-testing code.
Appreciate your time, I am yet developing my modus operandi with CMake,
so even shaping the right question is challenging for me right now.

issue when building module using cmake

i am trying to build box2D using cmake. When i click configure (and selecting my version of visual studio) cmake starts working but it finnishes with the error message:
You have called ADD_LIBRARY for library glfw without any source files.
This typically indicates a problem with your CMakeLists.txt file
Configuring done
What is the issue and how can i solve it? Where is the CMakeLists.txt located and how can i edit it to build without errors?
It is probably an error in this library. Try another version or post a bug.
It means that according to this file, a library should be created without source files (an empty library), which is probably bug.
CMakeLists.txt is located in every cmake source directory, make sure you're choosing the right directory.

Error in CMake path

I'm trying to build the Visual Studio project for a kinect demo thing, rgbddemo. According to the instructions on the page, I need to set the PATH variable to include QMAKE from QT. I did that, but I keep getting this error:
CMake Error at CMakeLists.txt:1 (QT4_WRAP_CPP):
Unknown CMake command "QT4_WRAP_CPP".
From what I could gather from google, it's a problem with CMake knowing where something from QT is. The page I linked above also mentions that you can set the path for QMAKE within CMake, but I don't know how to do that. Does anyone have any suggestions? Thanks.
You could try inserting the line
FIND_PACKAGE(Qt4)
into the top-level CMakeLists.txt file after the line
INCLUDE("${nestk_BINARY_DIR}/UseNestk.cmake")
That should cause it to try to find qmake for you. I'm not sure why they don't have that though, but then I'm not that familiar with cmake.
I think this lines in your CMakeLists.txt file can help you.
find_package(Qt4 Required)
include(${QT_USE_FILE}) #contains path to Qt header
#...
qt4_wrap_cpp(MOC_SOURCES ${MY_HEADERS}) #invoking moc
add_library(MY_LIB ${SOURCES} ${MOC_SOURCES}) #building lib
target_link_libraries(MY_LIB ${QT_LIBRARIES})
qt4_add_resources(MY_QT_RSC ${RESOURCES}) #if you want to compile from resource files
add_library(MY_LIB_2 ${MY_QT_RSC} {SOURCES})

How do I fix an apparently corrupted CMake build?

I am getting the following error from CMakeSetup on our source tree:
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_FIND_LIBRARY_PREFIXES
Deleting the cache doesn't help, so something in one of the CMakeLists must be the problem. The weird part is, if I copy in a CMakeCache.txt from an old version of the tree, and edit it so that the paths match, CMake will then configure successfully... and, even after deleting that fixed cache, it continues to configure successfully.
Any idea what I should look for?
There are two variables missing from the bad CMakeCache.txt when it's generated: Project_BINARY_DIR and Project_SOURCE_DIR.
Is your Project declared at the top and in your base CMakeLists.txt file or at the very least this has to be declared before it is needed, of which at the top is easiest.
It appears this is a bug in cmake. http://www.mail-archive.com/cmake#cmake.org/msg13392.html
i.e.
PROJECT(inkscape)
SET(INKSCAPE_VERSION 0.46+devel)
SET(PROJECT_NAME inkscape)
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.6)
SET(CMAKE_INCLUDE_CURRENT_DIR TRUE)
...