I am new to cmake and know this question has been asked before, but still cannot find what I am doing wrong. I have an external library with folders /include and lib. The /include folder contains all the headers (.h) and the /lib folder contains all the source (.c) files.
In my project I have this CMakeList.txt file:
cmake_minimum_required(VERSION 3.7)
project(FirstAttempt)
set(CMAKE_CXX_STANDARD 11)
set (EXTRA_LIBS "D:\\libtrading")
include_directories(${EXTRA_LIBS}/include)
link_directories(${EXTRA_LIBS}/include)
set(SOURCE_FILES main.cpp)
add_executable(FirstAttempt ${SOURCE_FILES})
target_link_libraries (FirstAttempt ${EXTRA_LIBS}/lib)
I know that I have to use target_link_libraries to link the source files of the library to my project, but certainly something is missing, but what? I am still receiving the error undefined reference to xxxxxx.
The library I am trying to include in my project is https://github.com/libtrading/libtrading.
Well, I'll try.
First, seems that you are calling link_directories() on the folder which contains header files, while it should be used in order to specify the path where to search libraries for.
Second, target_link_libraries() takes the absolute path of the shared/static library file as the second argument, while you are passing the directory path (well, it seems so).
target_link_libraries() doesn't link to the "source files of the library", - it links to the compiled shared/static library blob.
And, I would also recommend you to save the name of the executable to the variable so that you wouldn't be able to mistype the target name, like so:
set(TARGET FirstAttempt)
add_executable(${TARGET})
As you know, we need all source files to compile. So we need to point out Cmake know where/what are source files.
I think you should add all sources files like this
file(GLOB SOURCES_FILES "lib/*.c" main.cpp)
to add all .c files.
Or, you can add all lib/*.c files separately
file(SOURCES_FILES_LIBS "lib/*.c")
set(SOURCES_FILES main.cpp)
add_executable(FirstAttempt ${SOURCES_FILES_LIBS} ${SOURCES_FILES})
Related
I am new to cmake and know this question has been asked before, but still cannot find what I am doing wrong. I have an external library with folders /include and lib. The /include folder contains all the headers (.h) and the /lib folder contains all the source (.c) files.
In my project I have this CMakeList.txt file:
cmake_minimum_required(VERSION 3.7)
project(FirstAttempt)
set(CMAKE_CXX_STANDARD 11)
set (EXTRA_LIBS "D:\\libtrading")
include_directories(${EXTRA_LIBS}/include)
link_directories(${EXTRA_LIBS}/include)
set(SOURCE_FILES main.cpp)
add_executable(FirstAttempt ${SOURCE_FILES})
target_link_libraries (FirstAttempt ${EXTRA_LIBS}/lib)
I know that I have to use target_link_libraries to link the source files of the library to my project, but certainly something is missing, but what? I am still receiving the error undefined reference to xxxxxx.
The library I am trying to include in my project is https://github.com/libtrading/libtrading.
Well, I'll try.
First, seems that you are calling link_directories() on the folder which contains header files, while it should be used in order to specify the path where to search libraries for.
Second, target_link_libraries() takes the absolute path of the shared/static library file as the second argument, while you are passing the directory path (well, it seems so).
target_link_libraries() doesn't link to the "source files of the library", - it links to the compiled shared/static library blob.
And, I would also recommend you to save the name of the executable to the variable so that you wouldn't be able to mistype the target name, like so:
set(TARGET FirstAttempt)
add_executable(${TARGET})
As you know, we need all source files to compile. So we need to point out Cmake know where/what are source files.
I think you should add all sources files like this
file(GLOB SOURCES_FILES "lib/*.c" main.cpp)
to add all .c files.
Or, you can add all lib/*.c files separately
file(SOURCES_FILES_LIBS "lib/*.c")
set(SOURCES_FILES main.cpp)
add_executable(FirstAttempt ${SOURCES_FILES_LIBS} ${SOURCES_FILES})
I'm attempting to run a cmake hello world program on Windows 7 x64 with both Visual Studio 2010 and Cygwin, but can't seem to get either to work. My directory structure is as follows:
HelloWorld
-- CMakeLists.txt
-- src/
-- -- CMakeLists.txt
-- -- main.cpp
-- build/
I do a cd build followed by a cmake .., and get an error stating that
CMake Error: CMake can not determine linker language for target:helloworld
CMake Error: Cannot determine link language for target "helloworld".
However, if I change the extension of main.cpp to main.c both on my filsystem and in src/CMakeLists.txt everything works as expected. This is the case running from both the Visual Studio Command Prompt (Visual Studio Solution Generator) and the Cygwin Terminal (Unix Makefiles Generator).
Any idea why this code wouldn't work?
CMakeLists.txt
PROJECT(HelloWorld C)
cmake_minimum_required(VERSION 2.8)
# include the cmake modules directory
set(CMAKE_MODULE_PATH ${HelloWorld_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
add_subdirectory(src)
src/CMakeLists.txt
# Include the directory itself as a path to include directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Create a variable called helloworld_SOURCES containing all .cpp files:
set(HelloWorld_SOURCES main.cpp)
# Create an executable file called helloworld from sources:
add_executable(hello ${HelloWorld_SOURCES })
src/main.cpp
int main()
{
return 0;
}
I also got the error you mention:
CMake Error: CMake can not determine linker language for target:helloworld
CMake Error: Cannot determine link language for target "helloworld".
In my case this was due to having C++ files with the .cc extension.
If CMake is unable to determine the language of the code correctly you can use the following:
set_target_properties(hello PROPERTIES LINKER_LANGUAGE CXX)
The accepted answer that suggests appending the language to the project() statement simply adds more strict checking for what language is used (according to the documentation), but it wasn't helpful to me:
Optionally you can specify which languages your project supports.
Example languages are CXX (i.e. C++), C, Fortran, etc. By default C
and CXX are enabled. E.g. if you do not have a C++ compiler, you can
disable the check for it by explicitly listing the languages you want
to support, e.g. C. By using the special language "NONE" all checks
for any language can be disabled. If a variable exists called
CMAKE_PROJECT__INCLUDE_FILE, the file pointed to by that
variable will be included as the last step of the project command.
In my case, it was just because there were no source file in the target. All of my code was a template with the source code in the header file. Adding an empty file.cpp solved the problem.
Try changing
PROJECT(HelloWorld C)
into
PROJECT(HelloWorld C CXX)
or just
PROJECT(HelloWorld)
See: https://cmake.org/cmake/help/latest/command/project.html
Confusing as it might be, the error also happens when a cpp file included in the project does not exist.
If you list your source files in CMakeLists.txt and mistakenly type a file name then you get this error.
I want to add another solution in case a library without any source files shall be build. Such libraries are also known as header only libraries. By default add_library expects at least one source file added or otherwise the mentioned error occurs. Since header only libraries are quite common, cmake has the INTERFACE keyword to build such libraries. The INTERFACE keyword is used as shown below and it eliminates the need for empty source files added to the library.
add_library(myLibrary INTERFACE)
target_include_directories(myLibrary INTERFACE {CMAKE_CURRENT_SOURCE_DIR})
The example above would build a header only library including all header files in the same directory as the CMakeLists.txt. Replace {CMAKE_CURRENT_SOURCE_DIR} with a path in case your header files are in a different directory than the CMakeLists.txt file.
Have a look at this blog post or the cmake documentation for further info regarding header only libraries and cmake.
A bit unrelated answer to OP but for people like me with a somewhat similar problem.
Use Case: Ubuntu (C, Clion, Auto-completion):
I had the same error,
CMake Error: Cannot determine link language for target "hello".
set_target_properties(hello PROPERTIES LINKER_LANGUAGE C) help fixes that problem but the headers aren't included to the project and the autocompletion wont work.
This is what i had
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES ./)
add_executable(hello ${SOURCE_FILES})
set_target_properties(hello PROPERTIES LINKER_LANGUAGE C)
No errors but not what i needed, i realized including a single file as source will get me autocompletion as well as it will set the linker to C.
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES ./1_helloworld.c)
add_executable(hello ${SOURCE_FILES})
I also faced a similar error while compiling my C-based code. I fixed the issue by correcting the source file path in my cmake file. Please check the source file path of each source file mentioned in your cmake file. This might help you too.
Simply check the path to source file. (to the respective cpp)
By default the JNI Native folder is named as jni . Renaming it to cpp fixed the issue
I managed to solve mine, by changing
add_executable(file1.cpp)
to
add_executable(ProjectName file1.cpp)
I have a header-only library project set up with the cmake command:
add_library(my_library INTERFACE)
and I also added
target_sources(my_library INTERFACE ${MY_LIRBARY_HEADER_FILES})
but when I open a source file, I get the warning:
This file does not belong to any project target, code insight features might not work properly
and I lose a lot of the functionality on things like code completion.
What is the proper way to set this up so CLion provides its usual functionality on a header-only library?
Little background
I was having the same problem, albeit the project was not header-only, nevertheless, the open files from inc folder were throwing the aforementioned warning, even though the CMake file clearly marked that folder to be include_directory.
*.hpp files do not belong to ${SOURCE}
include_directories("${PROJECT_SOURCE_DIR}/inc/")
add_subdirectory(src)
add_executable(${EXECUTABLE_NAME} main.cpp ${SOURCE})
Since this is a perfectly valid CMake file and adding the include files to source files is not idiomatic, I did not want to amend the CMake file.
The solution
As described on the official JetBrains Forum, the CMake file is indeed valid and the warning is shown because of the inability of CLion to properly index header files. The suggested workaround extracted from the link is to right-click the folder and Mark directory as | Library Files/Project Sources and Headers.
So, this header isn't includes in executables and CLion notifies you that some code insight features might not work properly. As workaround you can use "Mark directory as" Library Files/Project Source and Headers for folder.
Clion takes information about source files from CMake build system. When you add any cpp file to sources list CMake automatically tell about header with same name. So if cpp/h names differs (or you don't have cpp file at all) you should include header manually.
set(Sources my_lib.cpp)
set(Headers header_of_my_lib.h)
add_executable(superlib ${Sources} ${Headers})
If you don't have any executable you can omit last line, CLion will still know about files
This warning is an IDE issue that Android Studio cannot recognise the current directory if it does not include any source files.
Workaround
Adding am empty source file, e.g empty_xxx.c under the directory in question and adding below line in your corresponding CMakeList.txt
add_library(${TARGET_NAME_XXX} SHARED ${SOME_DIR_HAVING_THIS_WARNING}/empty_xxx.c)
will help get rid of this warning.
You can add the header files to your project like this:
set(SOURCE_FILES main.cpp MyClass1.cpp MyClass1.h MyClass2.cpp MyClass2.h)
You can also set it in multiple steps like so:
set(SOURCE_FILES main.cpp)
set(SOURCE_FILES ${SOURCE_FILES} MyClass1.cpp MyClass1.h)
set(SOURCE_FILES ${SOURCE_FILES} MyClass2.cpp MyClass2.h)
Though as mentioned in the comments, you probably shouldn't be adding the header files to your project at all.
Regards:
I have two different C++ projects, each one with its own CMakeList.txt file that generates a .so library file. E.g. both projects are under the paths trunk/A, and trunk/B, and they contain the files:
trunk/A/CMakeList.txt
trunk/B/CMakeLists.txt
that respectively generate files A.so and B.so. For practical reasons, I want to keep both projects A and B separated, but I would also like to have a CMakeList.txt file (e.g. trunk/CMakeList.txt) that would compile both binaries simultaneously.
I expected to replicate something similar to what can be achieved with qmake, by including a .pri file in a .pro file. Hence, I tried by including both CMakeList files with the following code for trunk/CMakeList.txt:
PROJECT(TEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
include(A/CMakeLists.txt)
include(B/CMakeLists.txt)
But CMake does not work that way. The contents in the included CMakeList files are evaluated from the trunk directory. E.g, by reading the following line in trunk/A/CMakeList.txt:
FILE(GLOB HEADERS *.h)
CMake will only look for *.h files in /trunk, not in /trunk/A
Is there a proper way to do this in CMake? Something similar to what QMake does with .pri include files?
Hundred thanks in advance!.
You need to use ADD_SUBDIRECTORY:
PROJECT(TEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
ADD_SUBDIRECTORY( A )
ADD_SUBDIRECTORY( B )
I have created a project which consists of a main.cpp file and another class (named Physical) that is broken up into a Physical.hpp file and Physical.cpp file.
The project file structure looks like this:
main.cpp
header/Physical.hpp
src/Physical.cpp
The project compiles fine, but only if I include Physical.hpp in different ways depending on whether or not I'm including from the Physical.cpp or main.cpp file.
From the main file, I have to use:
#include "header/Physical.hpp"
Whereas, from the Physical.cpp file I have to use:
#include "../header/Physical.hpp"
Why is there this discrepancy? I expect it has something to do with my cmake configuration file, although I'm really not sure. Below is my cmake config file.
cmake_minimum_required(VERSION 2.8.4)
project(gravity_simulator ${PROJECT_BINARY_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp src/Physical.cpp)
include_directories("${PROJECT_BINARY_DIR}")
add_executable(gravity_simulator ${SOURCE_FILES})
# Detect and add SFML
set(CMAKE_MODULE_PATH "/usr/share/SFML/cmake/Modules/" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(gravity_simulator ${SFML_LIBRARIES})
endif()
Everything works as expected.
For your main.cpp file the header is located at header/Physical.hpp while for your Physical.cpp the header is located at ../header/Physical.hpp.
The mistake here is, that you think the relative path would always start from a specific directly, while instead every source file (*.cpp) gets compiled independently and thus the header search is happening for each file independently.
Personally if I don't write a library, I simply put all my source and header files in a src directory and everything will be fine. However if I'm writing a library, I use the <header.hpp> style and add the header directory to the include directory (include_directories("header")). The whole reason why one usually splits up things into header and source files is, so when one ships a library one can easily just ship the headers and doesn't have manually filter them out between the source files.
tl;dr When writing an application put everything a source directory. When writing a library, use the <> for the inclusion add the header directory to the include directories.