When trying to build my project, I see this in the build configurations (it pops up):
"LUCID" is the name of my project. I think it all built fine yesterday, but now after only restating I'm getting this:
Error: Target 'LUCID (LUCID)' not found.
The "Target" dropdown only has that one item in it (and also the "Build All" option). I do have project(LUCID) and add_executable(LUCID ${SOURCE_FILES}) in CMakeLists.txt, as was suggested in this question, although the situation is slightly different.
So, why am I getting this error and what do I do to fix it?
Another thing to note is that all the file names that should be part of my project and are specified in set(SOURCE_FILES ...) are greyed out in the CLion file browser, which they should not be.
I think you may put all you include_directory before add_executable.
And use only the find_package(SDL2 REQUIRED) futher more if you use the REQUIRED keyword you don't have to use the if (lib_FOUND) source here.
You CMake may look like something like this
cmake_minimum_required(VERSION 3.2)
project(LUCID)
set(EXEC_NAME LUCID)
MESSAGE("a test message")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package (Box2D REQUIRED)
find_package (opengl REQUIRED)
find_package (SDL2 REQUIRED)
set(INCLUDE_DIR
sinclude
sinclude/3rdparty
uniheader
D:/freetype-2.5.3/GnuWin32/include
${BOX2D_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS}
)
include_directories(${INCLUDE_DIR})
set(SOURCE_FILES
ssrc/Cam.cpp
#...
#Lots of source and header files in the same form
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
message(STATUS "Boaorm")
add_executable(${EXEC_NAME} ${SOURCE_FILES})
target_link_libraries(${EXEC_NAME} ${BOX2D_LIBRARIES} ${OPENGL_LIBRARIES} ${SDL2_LIBRARY})
For SDL i used this answer, but i don't like to use ${PROJECT_NAME} for executable name (you can choose what you prefer anyway)
Edit :
Multiple target_link_libraries are explained here
The problem with the old cmake was the include_directories after the add_executable and the common toolchain is include -> compile -> link then i just follow this logic.
Reset cache and reload project!
Tools > CMake > Reset Cache and Reload Project
I came across this weird bug yesterday. My CMakeLists.txt is correct (because I can build the project though the terminal).
The end of my CMakeLists.txt looks like this:
add_executable(assignment-1 main.cpp ${SOURCES})
add_library(libassignment-1 STATIC ${SOURCES})
I removed the CMake cache directory, commented out add_library() and reloaded it. Just like that, CLion can now find the assignment-1 executable. Then I uncommented the last line. All the configurations are still fine.
Related
I want to include gtest to my C++ project. I am using Clion as IDE, which should work. Some tests are already working, but I cannot use any functions from B_RocChoice.h. It says that the function is not declared in this scope.
Can somebody tell me what I am doing wrong? How I must change my CMakeLists.txt files that it recogizes my methods?
This is my basic_tests.cpp, where my testcases will be written.
This is my Directory.
Here, the most outer CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(cli)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(SOURCE_FILES
include/A_WowbaggerChoice.h
include/AbstractChoice.h
include/B_RocChoice.h
include/C_CnnChoice.h
include/D_DetectorChoice.h
include/E_LearningChoice.h
include/Help.h
include/MyException.h
include/StartScreen.h
include/tinyxml.h
include/types.h
src/A_WowbaggerChoice.cpp
src/AbstractChoice.cpp
src/B_RocChoice.cpp
src/C_CnnChoice.cpp
src/D_DetectorChoice.cpp
src/E_LearningChoice.cpp
src/Help.cpp
src/main.cpp
src/MyException.cpp
src/StartScreen.cpp
tinyxml/tinystr.cpp
tinyxml/tinystr.h
tinyxml/tinyxml.cpp
tinyxml/tinyxml.h)
add_subdirectory(googletest)
add_executable(cli ${SOURCE_FILES})
target_link_libraries( cli ${OpenCV_LIBS} )
CMakeLists.txt for gtest.
cmake_minimum_required(VERSION 2.6.2)
project( googletest-distribution )
enable_testing()
option(BUILD_GTEST "Builds the googletest subproject" ON)
#Note that googlemock target already builds googletest
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
if(BUILD_GMOCK)
add_subdirectory( googlemock )
elseif(BUILD_GTEST)
add_subdirectory( googletest )
endif()
add_subdirectory(basic_tests)
CMakeLists.txt for basic_tests
include_directories($(gtest_SOURCE_DIR}/include
${getest_SOURCE_DIR}))
#include_directories(../../src/)
include_directories(../../include/)
add_executable(runBasicCli
basic_check.cpp)
target_link_libraries(runBasicCli gtest gtest_main)
#target_link_libraries(cli)
I'm assuming your compiler is complaining it can't find the B_RocChoices.h header? Your question seems to imply the compiler error is about not finding a function, but B_RocChoices is a header and not a function in your basic_tests.cpp file.
Assuming your problem is that the compiler isn't finding the B_RocChoices.h header, I expect that when you include_directories(../../include) you are wanting to make the directory where B_RocChoices.h resides part of the header search path. This is a relative path, so it depends where the compiler is being run from as to what path it means (it wouldn't work if you were doing out of source builds, for example). Try using either CMAKE_SOURCE_DIR or CMAKE_CURRENT_SOURCE_DIR to define the path unambiguously. For example:
include_directories(${CMAKE_SOURCE_DIR}/include)
If you are using CMake 2.8.11 or later, I'd recommend you consider using target_include_directories() instead and probably also read up on target_link_libraries(). Together, these allow you to make the header search paths and linked libraries specific to a target rather than global to all targets. Lastly, if you prefer to download GoogleTest as part of your build rather than embedding it directly in your project sources, you may find the following link useful:
https://crascit.com/2015/07/25/cmake-gtest/
I'm using and like QtCreator to code and build my ROS projects written in c++.
Unfortunately the auto-completion for my own header files is not working: e.g. #include "LineTracker.hh"
Building the project works perfectly. And also the auto-completion for other external packages like ros or opencv is working.
Update 2.0: With QtCreator 3.6 the solution is not working
Update 1.0: Found a solution, see bottom!
Thats how my CMakeLists.txt looks:
cmake_minimum_required(VERSION 2.8.3)
project(line_tracking)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
tf
sensor_msgs
image_transport
cv_bridge
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
add_executable(${PROJECT_NAME}
src/line_tracking.cpp
src/EDLineDetector.cpp
src/LineTracker.cpp
)
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
# --- QT CREATOR STUFF ---
#Add all files in subdirectories of the project in
# a dummy_target so qtcreator has access to all files
FILE(GLOB children ${CMAKE_SOURCE_DIR}/*)
FOREACH(child ${children})
IF(IS_DIRECTORY ${child})
file(GLOB_RECURSE dir_files "${child}/*")
LIST(APPEND extra_files ${dir_files})
ENDIF()
ENDFOREACH()
add_custom_target(dummy_${PROJECT_NAME} SOURCES ${extra_files})
#
The file/package structure looks standard like this:
CMakeLists.txt
|
+ -- src
|
+ -- include
How do I have to adapt my CMakeLists.txt that QtCreator finds my headers for autocompletion?
Thank you very much for your help!
Sidenote:
When I use the top CMakeLists.txt file of the catkin workspace in QtCeator and I include the header files under their package path like this: #include <packageName/include/headerFile.h> the auto-completion is working but the build is not working anymore. So this is only a bad and not userfriendly hack to get auto-completion during coding.
Update 1.0:
I found a solution which is working:
I create a library from all the (class) files which have header files, and link the library to the main file, instead of adding the files as executables. I posted it here as answer.
But I don't know why it is working like this and not without the way over the library. Any explanations?
Update 2.0:
I just upgraded to QtCreator 3.6 and there my solution with the library in not working anymore.
Does anybody know another solution?!
Update: This solution does NOT work with QtCreator 3.6
I found a solution to my own question. Not so much fun, but anyway, I spent a lot of time with that issue so here the solution, which is hopefully useful for others:
Auto-completion with CMakeLists.txt in QtCreator 3.5.1 for your own classes:
Create a library with all your classes: ADD_LIBRARY(myFilesLib src/class1.cpp ...)
Add your executable (main function): add_executable(${PROJECT_NAME} src/main.cpp)
Link your library to your executable: target_link_libraries(${PROJECT_NAME} myFilesLib)
With this way over the library, auto-completion is working in QtCreator!
For ROS (catkin) don't forget to link the ${catkin_LIBRARIES}.
Here the whole CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(example_project)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
# Create a library with all your classes
add_library(myFilesLib
src/class1.cpp
src/class2.cpp
src/class3.cpp
)
target_link_libraries(myFilesLib
${catkin_LIBRARIES}
)
# add your executable
add_executable(${PROJECT_NAME}
src/main.cpp
)
# link the library with your classes to the executable
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
myFilesLib
)
I don't know why it is working only with the way over the library but it is working. Maybe somebody has an explanation?!
This is really annoying. Finally I figured out a solution, that works at Qt Creator 4.1.0, and probably works at other versions.
Write your include_directories correctly, so that compilation is OK.
Below the include_directories, add the following line. Note that the sequence matters, the following line must locate below the include_directories.
file(GLOB_RECURSE HEADERS */*.hpp */*.h)
Add ${HEADERS} # for qtcreator... in one of your add_executable. like:
add_executable(slam_pp_node
${HEADERS} # for qtcreator...
src/***.cpp
)
The above should be enough to solve the autocompletion problem. If not, add the following lines at the end of your CMakeList.txt:
file(GLOB_RECURSE EXTRA_FILES */*)
add_custom_target(${PROJECT_NAME}_OTHER_FILES ALL WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} SOURCES ${EXTRA_FILES})
Good luck!
I am trying to compile vMime by cmake, but I am getting error above,
I am using graphical interface of cmake and my makefiles.txt is below. It configures properly but do not generate
cmake_minimum_required(VERSION 2.8)
PROJECT(CXX)#vmime
enable_language(CXX)
set(VerifyCXX VerifyCXX.cxx)
add_definitions(-DVERIFY_CXX)
set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE Cxx)
add_executable(myapp vmime)
install(TARGETS myapp DESTINATION bin)
Help will be highly appreciated as I am stuck at point for couple of days.
CMake probably can not determine linker language for target myapp, because the target does not contain any source files with recognized extensions.
add_executable(myapp vmime)
should be probably replaced by
add_executable(myapp ${VerifyCXX})
Also this command
set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE Cxx)
cannot be succesfull, because ${TARGET} is used-before-set. You should call it after add_executable
set_target_properties(myapp PROPERTIES LINKER_LANGUAGE CXX)
Note that usually it is not needed at all.
For others' benefit, make sure you did not overlook an earlier error such as:
Cannot find source file:
MyFirstSourceFile.cpp
Another way to cause CMake to give you the error, "CMake Error: CMake can not determine linker language for target: myapp", is if you mistakenly point it exclusively at sources that do not exist.
For instance: I'm moving files from one directory to another and had the pre-move files with the post-move paths in my CMakeLists.txt. My output window is not very tall and I focused too soon on the "can not determine linker language" error!
I use clion IDE based on cmake, my source files named *.cc
project(server)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
file(GLOB SRC_FILE "main.cc" "listenfd.cc" "socket_util.cc"
"timers.cc" "network.cc" "main_event.cc")
add_executable(server ${server})
set_target_properties(server PROPERTIES LINKER_LANGUAGE CXX)
after I change
add_executable(server ${server}) to
add_executable(server "main.cc")
then I solved it, I really don't know why?
after experiment I found when use file(GLOB ....) like file(GLOB "src/main.cc") I must specify the relative path, then it works.
I was coding in KDevelop, ant I set up my CMakeLists.txt to include and link against libGL and freeglut.
However, it gave me the following error:
make[2]: *** No rule to make target '/usr/lib/libGL.so', needed by 'opengl'. Stop.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(opengl)
add_executable(opengl main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
target_link_libraries(opengl ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
First, what I am about to suggest might depend on CMake version, but I don't think (I'm using cmake version 3.1.0).
Looking at the installed FindOpenGL.cmake file, it looks like the variable you want in target_link_libraries is ${OPENGL_glu_LIBRARY}. Case does matter.
The best way to test an issue like this one is to use message("var: ${var}"), then you would be able to clearly see what your variable is set to during configuration.
Hope this helps!
FYI, I found the find module in the ${cmake_install_dir}/Modules/Find* where cmake_install_dir=/usr/share/cmake on my linux machine.
EDIT**
Sorry, was tired this morning and missed that you wanted GLUT, not GLU. There is a FindGLUT.cmake Module. You should call find_package(GLUT REQUIRED) and use ${GLUT_LIBRARIES} and ${GLUT_INCLUDE_DIR}. Sorry about the confusion!
I'm trying to learn cmake and have started converting an old make project over to cmake. Here is a simplified version of the directory structure I now have:
CMakeLists.txt
src/
CMakeLists.txt
main.cpp
core/
CMakeLists.txt
/sourcecode, other cmakes, etc.
test/
CMakeLists.txt
someTest.cpp
Currently, in my root CMakeLists.txt file I simply have this:
cmake_minimum_required(VERSION 2.8)
project(all)
add_subdirectory(src)
add_subdirectory(test)
What I wanted to do, was have a library created by core/CMakeLists.txt that can be used by both src/CMakeLists.txt to build the main executable, but also loaded by test/CMakeLists to build the unit tests.
So my src/core/CMakeLists.txt file currently looks sort of like this:
cmake_minimum_required(VERSION 2.8)
project(core)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wpedantic -Wreorder -DBOOST_TEST_DYN_LINK -DBOOST_LOG_DYN_LINK ")
#some other directories in my core code:
add_subdirectory(display)
add_subdirectory(training)
add_subdirectory(utility)
#some packages I use...
find_package(Boost 1.55.0
COMPONENTS
log
program_options
serialization
thread
system
filesystem
REQUIRED)
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${EIGEN3_INCLUDE_DIR})
target_link_libraries(core
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
So the idea is that I now have a core target I can simply link against to run my tests, and everything should work. However, when I try to build main, for example, I get:
Cannot specify link libraries for target "core" which is not built by this
project.
I thought this might be because core doesn't have a add_library command, but if I add add_library(core) I get this error:
You have called ADD_LIBRARY for library core without any source files. This typically indicates a problem with your CMakeLists.txt file
But I don't want to add any source files; I just want this target to link the targets in the core directory and produce a target I can link against from test.
Clearly I'm missing some core knowledge here, either with cmake or the toolchain itself. Help is appreciated :)
If you only want to create a core target without source files, you need to declare it like an INTERFACE target. So, try to add the following code to your src/core/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0) # REQUIRED 3.x.x version
project(core)
...
# Here declare your core_interface target
add_library(core_interface INTERFACE)
target_link_libraries(core_interface INTERFACE
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
As you can see, if you make this, you'll need to upgrade your CMake installed version.
Then, you'll build your tests or any executable, linking with this interface target directly:
add_executable(main main.cpp)
target_link_libraries(main core_interface)