CMake GLOB not returning any source files? - c++

I am trying to automatically have my Makefiles written for my C++ project using CMake with GLOB. The code and headers are however located in two separate folders.
/Users/username/Coding/Major Projects/ProjectName/Backend
and
/Users/username/Coding/Major Projects/ProjectName/Terminal
The backend has platform independent code. Just a bunch of c and c++ source files. And the Terminal folder has some code that uses the objects specified in Backend to run some tests on them. The reason these are in separate folders is that the Backend code is Multiplatform. So an Xcode project imports it etc. The Terminal folder has the testing code because that is the only one that is trying to compile it into a linux binary.
Anywho, I have the following CMakeList.txt file that I am trying to build to generate the Makefile.
cmake_minimum_required(VERSION 2.8.9)
project(terminalTest)
set(MainSource "/Users/username/Coding/Major Projects/ProjectName/Backend")
set(TerminalSource "/Users/username/Coding/Major Projects/ProjectName/Terminal")
#Bring the headers, such as Student.h into the project
include_directories(${MainSource} ${TerminalSource})
#Can manually add the sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Student.cpp)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "./{${MainSource},${TerminalSource}}/*.cpp")
add_executable(terminalTest ${SOURCES})
And the result of this when I run this from the CMake GUI is a successful configure but an error No SOURCES given to target: terminalTest meaning that my file() command is not working properly.
I think it could have something to do with the fact that I have spaces in my paths but that doesn't seem to do it either. By the way I am putting this file in the Terminal folder and attempting to build from Terminal/Build.
Is there any way to debug and see what sources the GLOB command is bringing in? Can I actually do a multi directory GLOB like this?

Your file(GLOB ...) path looks malformed. You can list the paths to your sources separately in this command to grab all the source files in both directories.
file(GLOB SOURCES
${MainSource}/*.cpp
${TerminalSource}/*.cpp
)

Related

cmake + cpp: No such file or directory

I've I'm trying to build this "Hello World" wxWidgets example on Linux, using the following cmake script:
cmake_minimum_required (VERSION 2.6)
project (wxL)
find_package(wxWidgets 3.0.0 REQUIRED
COMPONENTS base core net xml html adv qa richtext
)
file(GLOB SOURCES "src/*.cpp")
add_executable(wxL ${SOURCES})
Building the project yields this error:
src/wxL.cpp:3:10: fatal error: wx/wxprec.h: No such file or directory
The file specified in the include, wx/wxprec.h can be found on disk at this location:
/usr/include/wx-3.0/wx/wxprec.h
Furthermore, another program that I have built from source includes the same file (also using cmake) and builds just fine.
So, how do I use cmake to tell the compiler that the file should be included from somewhere in the system directories?
I know I'm missing something basic, but I can't figure out what.
Although you've found the package, your executable does not know anything about it.
For the executable to compile correctly, it needs to find header files for your package together with the .so / .a files. Following example should get you started:
include_directories(${wxWidgets_INCLUDE_DIRS})
add_executable(wxL <add-source-files-here>)
target_link_libraries(wxL ${wxWidgets_LIBRARIES}) // links wxWidgets libraries to your executable
Please note that using glob is not a recommended way of adding source files to your project.

How to display files generated to build space in QtCreator

I use a CMakeLists.txt like the following
cmake_minimum_required(VERSION 3.5)
project(generate_files_from_tool)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_custom_command(
OUTPUT generated_config.cpp
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/writeSourceFileFromConfigXml.sh ${CMAKE_CURRENT_SOURCE_DIR}/config.xml > ${CMAKE_CURRENT_BINARY_DIR}/generated_config.cpp
DEPENDS config.xml writeSourceFileFromConfigXml.sh
)
add_executable(tool_with_generated_file main.cpp generated_config.cpp)
which generates a file (here generated-config.cpp) in the build process using a custom script (writeSourceFileFromConfigXml.sh).
I can open it using QtCreator 4.4.1 just fine: Compile and run works -- but I cannot see the file generated_config.cpp in the Projects view, only CMakeLists.txt and main.cpp is visible there.
If I open the file "manually", QtCreator displays a warning "This file is not part of any project." -- but it uses the file for the compilation. And after opening the file, I'm able to switch between definition and implementation using Ctrl+Klick or F2 (and get to/come from generated_config.cpp)
So my question is: How can I make QtCreator understand, that this generated file is part of the project and add it to the Projects view?
Note: changing the last line to add_executable(tool_with_generated_file main.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated_config.cpp) did not change anything.
I am using a workaround to show generated sources located outside of build directory:
I have symlinks created in PROJECT_SOURCE_DIR to directories where generated sources are located, say src1, src2 (it is important to not use real paths). Then I use :
file(GLOB SOURCE_FILES ${PROJECT_SOURCE_DIR}/src1/*.[ch] ${PROJECT_SOURCE_DIR}/src2/*.[ch])
add_custom_target(
all_src
SOURCES ${SOURCE_FILES}
)

CMake and VisualStudio: Group files in solution explorer

To finish a long coding session on a project, I wanted to test if my CPP project is compilable on an arrangement of OS'es.
I've been working in Win10 all the time. Compiles fine.
I've tried a Raspberry Pi. Compiles fine.
I re-download a seperate copy of my project to a Win10 client, run cmake-gui, and open the project: My folder structure in the solution explorer all gone.
So I started digging around, and apparently this structure is kept in CMakeLists.txt with the command source_group. So I start adding more source_groupings to my cmake lists, and for some reason my groupings won't take.
Example:
source_group("game\\entitysystem" FILES ${entitysystem_SRC}) // Existing grouping
source_group("game\\entitysystem\\components" FILES ${components_SRC}) // My new grouping
My glob would be this:
file(GLOB components_SRC
"game/components/*.h"
"game/components/*.cpp"
)
file(GLOB entitysystem_SRC
"game/entitysystem/*.h"
"game/entitysystem/*.cpp"
)
I do believe my GLOB's are correct since the new project-clone compiles fine. It's just that every part of the new structure in Visual Studio's Solution Explorer seems lost. Yes, I have cleared Cmake's cache and regenerated the project. Doesn't change it.
Original structure:
Cloned project structure:
Edit:
I did make a mistake in my source_group as in that it should not put components beneath entitysystem, but still, why aren't there any filters created in Visual Studio?
First, make sure you are setting set_property(GLOBAL PROPERTY USE_FOLDERS ON).
Second, it is not recommended to use GLOB to collect a list of source files. From the file(GLOB documentation:
We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
The recommended way to list project files is to add them by hand to CMakeLists.txt.
If you still want to GLOB, it looks like you want to mirror the directory structure in your source tree. You can use a macro such as this every place you define a library or executable to automatically sort them for you:
foreach(FILE ${SRCS})
# Get the directory of the source file
get_filename_component(PARENT_DIR "${FILE}" DIRECTORY)
# Remove common directory prefix to make the group
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")
# Make sure we are using windows slashes
string(REPLACE "/" "\\" GROUP "${GROUP}")
# Group into "Source Files" and "Header Files"
if ("${FILE}" MATCHES ".*\\.cpp")
set(GROUP "Source Files${GROUP}")
elseif("${FILE}" MATCHES ".*\\.h")
set(GROUP "Header Files${GROUP}")
endif()
source_group("${GROUP}" FILES "${FILE}")
endforeach()

CMake -- Add all sources in subdirectory to cmake project

As a follow up to this question:
Add Source in a subdirectory to a cmake project
What is the best way (perhaps using the FILE directive?) to select all the .cpp and .h files in the subdirectory and add them to the SOURCE variable defined in the parent directory?
Example from answer to question above:
set(SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
PARENT_SCOPE
)
set(HEADERS
${HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
PARENT_SCOPE
)
Is it possible to do something like this?
FILE(GLOB SUB_SOURCES *.cpp)
set(SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/${SUB_SOURCES}
PARENT_SCOPE
)
What is the best way (using CMake) to compile all the sources in a directory and a subdirectory into a single output file (not multiple libraries?)
I think what you are looking for is the aux_source_directory command.
aux_source_directory Find all source files in a directory.
aux_source_directory( )
Collects the names of all the source files in the specified directory
and stores the list in the provided. This command is
intended to be used by projects that use explicit template
instantiation. Template instantiation files can be stored in a
"Templates" subdirectory and collected automatically using this
command to avoid manually listing all instantiations.
It is tempting to use this command to avoid writing the list of source
files for a library or executable target. While this seems to work,
there is no way for CMake to generate a build system that knows when a
new source file has been added. Normally the generated build system
knows when it needs to rerun CMake because the CMakeLists.txt file is
modified to add a new source. When the source is just added to the
directory without modifying this file, one would have to manually
rerun CMake to generate a build system incorporating the new file.
Your CMakeLists.txt within the subdirectory could look like this:
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SUB_SOURCES)
set(SOURCE
${SOURCE}
${SUB_SOURCES}
PARENT_SCOPE
)
The recommended practice is however, as you see from the documentation, to list the files individually within CMakeLists.txt as changes to the CMakeLists.txt file triggers running cmake.
I hope this was helpful and to the point.

How to integrate QT internationalization to CMake?

Greetings all,
I am trying to use QT internationalization with CMake.
I have configured my cmake file as follows :
#Internalization - this should generate core_jp.ts ?
SET(rinzo_core_TRANSLATIONS
i18n/core_jp.ts
)
#these are my source files in the project
SET(FILES_TO_TRANSLATE
${rinzo_core_srcs}
${rinzo_core_moh_srcs}
)
QT4_CREATE_TRANSLATION(QM_FILES ${FILES_TO_TRANSLATE} ${rinzo_core_TRANSLATIONS})
QT4_ADD_TRANSLATION(QM ${rinzo_core_TRANSLATIONS})
But it doesnt genereate any TS nor QM files.
My questions -
1.Does Cmake(by using QT tools) generate TS files automatically extracting "tr()" methods from the source ? (that means I dont have to create any TS file and above i18n/core_jp.ts will be genereated automatically)
2.What exacly are QM files ?
Thanks in advance
In CMake documentation see QT4_CREATE_TRANSLATION and QT4_ADD_TRANSLATION macros.
So you should do the followings:
SET(Lang_files
example.ts
)
...
QT4_CREATE_TRANSLATION(LangSrcs ${Lang_files})
...
ADD_EXECUTABLE(project_name ... others sources ... ${LangSrcs})
Translation binary files (*.qm) according to http://itk.org/Wiki/CMake:How_To_Build_Qt4_Software
Also from the bottom of that website
Usage - Updating the .ts files
When you want it to process all your
source files (looking for new texts to
translate), configure cmake to turn on
UPDATE_TRANSLATIONS, and then make
your project. CMake will modify your
.ts files in your SOURCE folders in
addition to generating the .qm files.
WARNING: Be aware that CMake will be updating the source .ts files, which means that if > you do a make clean, it will DELETE your source .ts files!!! So it would be a good idea > to switch off UPDATE_TRANSLATIONS as soon as possible.
My solution relies on manually invoked lupdate and lrelease tools via add_custom_target, so the generated files are not removed on make clean and put into the source directory.
I defined a function that scans provided directory, generates/updates ts files, and compiles them into qm files in the same directory, so they can be added to the app via .qrc file
cmake_minimum_required(VERSION 3.5)
project(l10n LANGUAGES CXX)
find_package(Qt5 COMPONENTS Core LinguistTools REQUIRED)
# genearats ts and qm file searching recursively SRC_DIR
function(generate_translations CUSTOM_TARGET TS_DIR TS_FILES SRC_DIR)
set(UPADTE_TS_TARGET_NAME ${CUSTOM_TARGET}_ts)
set(UPADTE_QM_TARGET_NAME ${CUSTOM_TARGET}_qm)
add_custom_target(${UPADTE_TS_TARGET_NAME}
COMMAND ${Qt5_LUPDATE_EXECUTABLE} -recursive ${SRC_DIR} -ts ${TS_FILES}
WORKING_DIRECTORY ${TS_DIR})
add_custom_target(${UPADTE_QM_TARGET_NAME}
COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS_FILES}
WORKING_DIRECTORY ${TS_DIR})
add_dependencies(${UPADTE_QM_TARGET_NAME} ${UPADTE_TS_TARGET_NAME} )
add_dependencies(${CUSTOM_TARGET} ${UPADTE_QM_TARGET_NAME})
endfunction()
add_executable(l10n main.cxx)
target_link_libraries(l10n Qt5::Core)
set(MY_TS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/translate)
set(MY_TS_FILES foo_en.ts foo_en.ts)
set(MY_SOURCE_LOOKUP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
generate_translations(l10n "${MY_TS_DIR}" "${MY_TS_FILES}" "${MY_SOURCE_LOOKUP_DIR}")
can you use lupdate.exe, linguist.exe and lrelease.exe from qt/[qt_version]/[msvc|mingw|...]/bin/ ?
you can use it like that:
Usage:
lupdate [options] [project-file]
lupdate [options] [source-file|path]... -ts ts-files
Options:
-help Display this information and exit.
-noobsolete
Drop all obsolete strings.
-extensions [,]...
Process files with the given extensions only.
The extension list must be separated with commas, not with whitespace.
Default: 'ui,c,c++,cc,cpp,cxx,ch,h,h++,hh,hpp,hxx'.
-pluralonly
Only include plural form messages.
-silent
Do not explain what is being done.
-version
Display the version of lupdate and exit
so, [source-file|path] - is you option, like i think.
try to call it with list of source files names.