Make References like Visual Studio in CMake - cmake

I start to use CMake recently and I starting to get used to the program. I managed to generate two Visual Studio solutions/projects: a shared library (core.dll) and a static library (zlib.lib). Now I still have a problem, my core.dll library makes references to the zlib library in Visual Studio. (In Properties -> Common Properties -> References). How can I reproduce this?
Here is how my files are located:
project
---core
------src
------CMakeLists.txt
---external
------zlib
------CMakeLists.txt
---------zlib-1.2.8
And here my two CMakeLists :
For core.dll
cmake_minimum_required(VERSION 2.8)
#Configuration project
project(core CXX)
#add definitions
#set(LIBRARY_OUTPUT_PATH_DEBUG vc2013_x64/bin/)
set(BIN_PATH "../vc2013_x64d")
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")#"/GL" "/Gy"
#remove_definitions
add_definitions(-UCMAKE_INTDIR="Release")
#Configuration Library
add_library(
core
SHARED
... .h and .cpp
)
For zlib.lib
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
#Configuration de ZLib
PROJECT(zlib C)
SET(BIN_PATH "../core/vc2013_x64d")
SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE C)
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /W3")
ADD_DEFINITIONS("/Gm" -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_UNICODE)
ADD_LIBRARY(
zlib
STATIC
... .h and .c
)
EDIT :
It's work like this :
cmake_minimum_required(VERSION 2.8)
#Configuration de Zlib.lib
PROJECT(zlib C)
SET(BIN_PATH "../cmakex64d")
#SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE C)
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/lib/" )
SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /W3")
ADD_DEFINITIONS("/Gm" -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_UNICODE)
ADD_LIBRARY(
zlib
STATIC
external/zlib/zlib-1.2.8/adler32.c
external/zlib/zlib-1.2.8/compress.c
external/zlib/zlib-1.2.8/crc32.c
external/zlib/zlib-1.2.8/deflate.c
external/zlib/zlib-1.2.8/gzclose.c
external/zlib/zlib-1.2.8/gzlib.c
external/zlib/zlib-1.2.8/gzread.c
external/zlib/zlib-1.2.8/gzwrite.c
external/zlib/zlib-1.2.8/infback.c
external/zlib/zlib-1.2.8/inffast.c
external/zlib/zlib-1.2.8/inflate.c
external/zlib/zlib-1.2.8/inftrees.c
external/zlib/zlib-1.2.8/trees.c
external/zlib/zlib-1.2.8/uncompr.c
external/zlib/zlib-1.2.8/zutil.c
external/zlib/zlib-1.2.8/crc32.h
external/zlib/zlib-1.2.8/deflate.h
external/zlib/zlib-1.2.8/gzguts.h
external/zlib/zlib-1.2.8/inffast.h
external/zlib/zlib-1.2.8/inffixed.h
external/zlib/zlib-1.2.8/inflate.h
external/zlib/zlib-1.2.8/inftrees.h
external/zlib/zlib-1.2.8/trees.h
external/zlib/zlib-1.2.8/zlib.h
external/zlib/zlib-1.2.8/zutil.h
)
project(core CXX)
set(BIN_PATH "../cmakex64d")
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
set_property(GLOBAL PROPERTY LINK_LIBRARY_DEPENDENCIES "yes")
add_definitions(-D_UNICODE -D_USRDLL -DCORE_EXPORTS)
add_definitions("/Gm")
#Configuration de core.dll
add_library(
core
SHARED
core/src/asserts.h
core/src/errors.h
core/src/filepath.h
core/src/memory.h
core/src/resources.h
core/src/stdafx.h
core/src/streams.h
core/src/string.h
core/src/system.h
core/src/variants.h
core/src/filepath.cpp
core/src/main.cpp
core/src/memory.cpp
core/src/resources.cpp
core/src/stdafx.cpp
core/src/streams.cpp
core/src/string.cpp
core/src/system.cpp
)
link_directories("/build/lib/")
target_link_libraries(core zlib)
But when i "configure" in CMake i've this warning now :
CMake Warning (dev) at C:/Program Files
(x86)/CMake/share/cmake-3.1/Modules/CMakeDetermineCXXCompiler.cmake:106
(if): Policy CMP0054 is not set: Only interpret if() arguments as
variables or keywords when unquoted. Run "cmake --help-policy
CMP0054" for policy details. Use the cmake_policy command to set
the policy and suppress this warning.
Quoted variables like "MSVC" will no longer be dereferenced when the
policy is set to NEW. Since the policy is not set the OLD behavior
will be used. Call Stack (most recent call first): CMakeLists.txt:51
(project) This warning is for project developers. Use -Wno-dev to
suppress it.
CMake Warning (dev) at C:/Program Files
(x86)/CMake/share/cmake-3.1/Modules/CMakeFindBinUtils.cmake:33 (if):
Policy CMP0054 is not set: Only interpret if() arguments as variables
or keywords when unquoted. Run "cmake --help-policy CMP0054" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
Quoted variables like "MSVC" will no longer be dereferenced when the
policy is set to NEW. Since the policy is not set the OLD behavior
will be used. Call Stack (most recent call first): C:/Program Files
(x86)/CMake/share/cmake-3.1/Modules/CMakeDetermineCXXCompiler.cmake:162
(include) CMakeLists.txt:51 (project) This warning is for project
developers. Use -Wno-dev to suppress it.
Why ? I've not that before ?

Related

How to configure CMakeLists.txt for OpenMP?

I am building a python extension from c++ shared library.
This library is using some openmp pragma.
I would like to know how to configure CMakeLists.txt in order to include openmp ?
I have added the openmp flag -fopenmp
But I still have this error : undefined symbol: GOMP_critical_end
here is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++lastest -pthread -fopenmp")
project (py_interface)
#find_library('gomp')
find_package(OpenMP REQUIRED)
find_package(Boost REQUIRED)
include_directories(/usr/include/python3.6/)
link_directories(/usr/local/lib)
set(SRC interface.cpp)
add_library(py_interface SHARED ${SRC})
target_link_libraries(py_interface PRIVATE OpenMP::OpenMP_CXX ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
set_property(TARGET py_interface PROPERTY POSITION_INDEPENDENT_CODE ON)

CLion parses GCC errors?

I have a few CMake targets in my project that calls makefiles, which will call gcc.
When there is a build error it shows up on Messages>Build:
my_dir/my_file.c: In function 'my_func':
my_dir/my_file.c.c:52:5: error: expected ';' before '}' token
I wanted to click on it and go straight to my_dir/my_file.c.c:52:5, is that possible?
My Cmakelists.txt only has symbols and include folders for indexing purposes, no C build configuration since all of that is made in the makefiles.
EDIT
This is my CMakeLists.txt structure:
cmake_minimum_required(VERSION 3.7)
project(myexec)
set(CMAKE_CXX_STANDARD 99)
set(MakefileDir ${CMAKE_CURRENT_SOURCE_DIR}/make)
add_custom_target(
mytarget
ALL
WORKING_DIRECTORY ${MakefileDir}
COMMAND make all)
file(GLOB_RECURSE SRCS *.c)
file(GLOB_RECURSE HDRS *.h)
include_directories(
sometthing/inc
(...)
)
add_definitions(
-DMY_SYM=1
(...)
)
add_executable(myexec EXCLUDE_FROM_ALL ${SRCS} ${HDRS})

CMake, shared library linking fail

I'm currently getting used to cmake and I'm trying to compile a small project with a .so library linking.
My project is the following.
/
CMakeLists.txt
inc/
Als.h
src/
main.c
CMakeLists.txt
lib/
libals.so
build/
I'm compiling from the build directory with:
$ cmake ..
-- DIR:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/julien/tmp/cmakeTest/build
And then:
$ make
Linking C executable cmakeTest
/usr/bin/ld: ne peut trouver -lals
collect2: error: ld returned 1 exit status
src/CMakeFiles/cmakeTest.dir/build.make:85: recipe for target 'src/cmakeTest' failed
make[2]: *** [src/cmakeTest] Error 1
CMakeFiles/Makefile2:75: recipe for target 'src/CMakeFiles/cmakeTest.dir/all' failed
make[1]: *** [src/CMakeFiles/cmakeTest.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
The linker seems to be unable to find the libals.so file.
Here is the file /CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
PROJECT(cmaketest)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
INCLUDE_DIRECTORIES(
inc
)
ADD_SUBDIRECTORY(src)
get_directory_property(OUT_VAR LINK_DIRECTORIES)
message(STATUS "DIR: ${OUT_VAR}")
And here is the file /src/CMakeLists.txt:
PROJECT(cmakeTest)
FILE(
GLOB
${PROJECT_NAME}_Sources
*.c
)
INCLUDE_DIRECTORIES(
inc
inc
)
ADD_EXECUTABLE(
${PROJECT_NAME}
${${PROJECT_NAME}_Sources}
)
LINK_DIRECTORIES(
/home/julien/tmp/cmakeTest/lib/
)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
als
pthread
)
Maybe I missed something but if I change the /src/CMakeLists.txt to:
PROJECT(cmakeTest)
FILE(
GLOB
${PROJECT_NAME}_Sources
*.c
)
INCLUDE_DIRECTORIES(
inc
inc
)
ADD_EXECUTABLE(
${PROJECT_NAME}
${${PROJECT_NAME}_Sources}
)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
/home/julien/tmp/cmakeTest/lib/libals.so
pthread
)
The compilation is ok. Does someone know why the linker is unable to find libals.so when I'm giving him the good directory path to look in?
The LINK_DIRECTORIES functions seems not to be working.
Besides the solution you already have, and my solution in a comment, the problem you have with the CMake file shown is the order in which you invoke the CMake commands.
From the link_directories command reference:
The command will apply only to targets created after it is called.
[Emphasis mine]
You simply need to call link_directories before you call add_executable.

Building a project with SFML library using CMake

Similar to this question I'm trying to build a project with SFML using CMake.
My CMakeLists.txt file is:
cmake_minimum_required(VERSION 2.6)
project(pong)
# Specify C++11 flag for g++
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2")
endif()
# Add directory containing FindSFML.cmake to module path
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH})
# Add sources
file(GLOB SOURCES
"${PROJECT_SOURCE_DIR}/*.cpp"
)
# Find SFML
set(SFML_ROOT "C:/Users/usr/Downloads/_ Downloads (new)_/SFML-2.4.2-linux-gcc-64-bit/SFML-2.4.2")
find_package( SFML COMPONENTS audio graphics window system REQUIRED )
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(pong ${SFML_LIBRARIES})
else()
#set(SFML_ROOT "" CACHE PATH "SFML top-level directory")
message("\n-> SFML directory not found. Set SFML_ROOT to SFML's top-level path (containing \"include\" and \"lib\"
directories).")
message("-> Make sure the SFML libraries with the same configuration (Release/Debug, Static/Dynamic) exist.\n")
endif()
add_executable(pong ${SOURCES})
When I try to build it [using MinGW-w64 + MSYS2] on Windows, I get the following error:
CMake Error at cmake/Modules/FindSFML.cmake:307 (message):
Could NOT find SFML (missing: SFML_AUDIO_LIBRARY SFML_GRAPHICS_LIBRARY
SFML_WINDOW_LIBRARY SFML_SYSTEM_LIBRARY)
Call Stack (most recent call first):
CMakeLists.txt:19 (find_package)
I don't know why the libraries are not found.
Please help me.

Cmake failed to find Threads package with cryptic error message

I'm trying to configure with following CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(MotionBlow CXX)
find_package(Threads REQUIRED)
find_package(Boost COMPONENTS system program_options REQUIRED)
include(gtest.cmake)
add_executable(motionBlow src/blow.cpp)
target_include_directories(motionBlow PUBLIC include)
target_link_libraries(motionBlow RTIMULib ${Boost_LIBRARIES})
set_property(TARGET motionBlow PROPERTY CXX_STANDARD 14)
add_executable(chat_client src/chat_client.cpp)
target_include_directories(chat_client PUBLIC include)
target_link_libraries(chat_client ${Boost_LIBRARIES} Threads::Threads)
set_property(TARGET chat_client PROPERTY CXX_STANDARD 14)
add_executable(chat_server src/chat_server.cpp)
target_include_directories(chat_server PUBLIC include)
target_link_libraries(chat_server ${Boost_LIBRARIES} Threads::Threads)
set_property(TARGET chat_server PROPERTY CXX_STANDARD 14)
enable_testing()
add_executable(matrixTest test/MatrixTest.cpp src/Matrix.cpp)
target_include_directories(matrixTest PUBLIC include ${GTEST_INCLUDE_DIR})
target_link_libraries(matrixTest ${GTEST_LIBRARY} Threads::Threads)
set_property(TARGET matrixTest PROPERTY CXX_STANDARD 14)
On ubunutu 16.04 it works ok, but both on raspberry pi with Raspbian 8.0/Cmake 3.6.2 and on ubuntu 15.10/Cmake 3.2.2 I get
CMake Error at /usr/share/cmake-3.2/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/usr/share/cmake-3.2/Modules/FindPackageHandleStandardArgs.cmake:374 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.2/Modules/FindThreads.cmake:204 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:4 (find_package)
Unfortunately, error log contains only this:
Determining if files pthread.h exist failed with the following output:
Source:
/* */
#include <pthread.h>
int main(void){return 0;}
So I have no idea how to get this fixed. Any ideas? Is my CMakelists.txt missing something or should I get a missing package?
Taking your code I could reproduce your error and it seems to be a follow-up error from this:
-- Looking for include file pthread.h
CMake Error at /usr/share/cmake-3.2/Modules/CheckIncludeFiles.cmake:74 (try_compile):
Unknown extension ".c" for file
try_compile() works only for enabled languages. Currently these are:
CXX
See project() command to enable other languages.
Two possible solutions:
Add C to your project languages:
project(MotionBlow C CXX)
Add .c extension a valid C++ file:
MotionBlowMakeRulesOverwrite.cmake
list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)
CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
set(CMAKE_USER_MAKE_RULES_OVERRIDE "MotionBlowMakeRulesOverwrite.cmake")
project(MotionBlow CXX)
If none of this works, check that pthread is installed:
sudo apt-get install libpthread-stubs0-dev
References
Tell CMake to use C++ compiler for C files coming from CMake?
Unable to locate package pthread