CMake: Not Connecting -lrt to project - cmake

I have a project, dependent on the rt library, that shows undeclared errors for functions ddefined in LIBRT when using make following running the "cmake .". I've looked at the plethora of suggestions for questions similar to this but none worked for me. Using the functions check_include_files() and check_library_exists() return TRUE, and it appears I succeed in finding the library using find_library(). I even get "-lrt" appended at the end of the link.txt file.
Anyways, I am a bit stumped and looking for some guidance. Below is my CMakeLists.txt file:
cmake_minimum_required (VERSION 3.5)
project (proj_name C)
enable_language(C)
set (SOURCES
/source/file/path
)
add_definitions (-std=c99)
add_executable (proj_name ${SOURCES})
find_library(LIBRT rt)
if(LIBRT)
target_include_directories (proj_name PUBLIC ${PROJECT_SOURCE_DIR}
/proj/includes/path #not including rt header path
${RT_INCLUDE_DIRS}
)
target_link_libraries(proj_name PUBLIC ${LIBRT})
endif(LIBRT)
set(CMAKE_C_COMPILER /usr/bin/gcc)
include(CheckIncludeFiles)
check_include_files(mqueue.h HAVE_MQUEUE_H)
if(NOT HAVE_MQUEUE_H)
message(FATAL_ERROR "mqueue.h not found")
endif(NOT HAVE_MQUEUE_H)
include(CheckLibraryExists)
check_library_exists(rt timer_gettime "" HAVE_RT)
if(NOT HAVE_RT)
message(FATAL_ERROR "timer_gettime not found")
endif(NOT HAVE_RT)
Running on WSL Ubuntu.
Thanks for any insight possible!

Related

cmake linker configuration for a project that uses wxWidgets

I have the following problem. I want to create a cross-platform GUI application that uses wxWidgets
I am trying to compile and run a "Hello World" example from wxWidgets web site.
I want to use cmake to build the project. On Linux everything works without problems, but on Windows I see the following linking error
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
It looks like the linker is looking for main function for some reason (shouldn't it look for WinMain?)
The example code has wxIMPLEMENT_APP(MyApp); macro, I have also set the linker option
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows"), but nothing helps, so I don't understand what is the problem. I have a feeling that I missed something simple. May be you had a similar problem before with one of your projects? Thank you for your help.
My CMakeLists.txt file is given below
cmake_minimum_required (VERSION 3.8)
project ("project")
set(CMAKE_CXX_STANDARD 17)
set(WXWIN "C:/msys64/wxWidgets")
# =========================================================================
set(BUILD_DEPS ON)
list(APPEND CMAKE_MODULE_PATH "include/libs")
include_directories("include/libs")
add_subdirectory("include/libs/glog")
if (MSVC)
set(wxWidgets_ROOT_DIR ${WXWIN})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows")
endif (MSVC)
find_package(wxWidgets COMPONENTS net gl core base)
include(${wxWidgets_USE_FILE})
set(wxWidgets_USE_UNICODE ON)
# Create shared library for the project
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(PROJECT_LINK_LIBS Tools)
set(LIBRARY_SRC include/Tools.cpp)
add_library(Tools SHARED ${LIBRARY_SRC})
link_directories("out/build")
set_target_properties(Tools PROPERTIES POSITION_INDEPENDENT_CODE 1)
# =========================================================================
# Add source to this project's executable.
add_executable (project "project.cpp" "project.h")
target_compile_features(project PUBLIC cxx_std_17)
target_link_libraries (project ${PROJECT_LINK_LIBS} ${wxWidgets_LIBRARIES})
It turns out that to make it work in need to put WIN32 into add_executable command of cmake
add_executable (project "project.cpp" "project.h") => add_executable (project WIN32 "project.cpp" "project.h")

How to tell CMake to find the lib in local folder through command line?

I am trying to link my local version of gflags (which is in ~/mylibs/gflags) while compiling google-test. In gtest's CMakeLists.txt, it's using find_package:
if (WITH_GFLAGS)
find_package (gflags 2.2.0)
if (gflags_FOUND)
set (HAVE_LIB_GFLAGS 1)
determine_gflags_namespace (gflags_NAMESPACE)
endif (gflags_FOUND)
endif (WITH_GFLAGS)
I don't want to modify the CMakeLists.txt file. Is there anyway to tell cmake to find the package in my folder ?
Thanks!
The gflags documentation describes specifying the location of a non-standard installation when using CMake:
The gflags_DIR variable must be set to the <prefix>/lib/cmake/gflags directory containing the gflags-config.cmake file
In your case, this might look like:
cmake -Dgflags_DIR=/home/blackball/mylibs/gflags/lib/cmake/gflags [...]
Since you are integrating gflags as subproject in a super cmake, you basically want find_package() to be a no-op if you have it in your "meta-project" source dir. This can be done by overloading the find_package() command.
Top CMakeLists.txt:
# Use find_package everywhere, no-op if it's a subproject
macro(find_package)
if(NOT TARGET ${ARGV0} AND NOT TARGET ${ARGV0}::${ARGV0})
_find_package(${ARGV})
else()
if(TARGET ${ARGV0})
get_target_property(TGT_VER ${ARGV0} VERSION)
set(TGT ${ARGV0})
else()
get_target_property(TGT_VER ${ARGV0}::${ARGV0} VERSION)
set(TGT ${ARGV0}::${ARGV0})
endif()
message(STATUS "Found ${ARGV0}: CMake Target ${TGT} (found version \"${TGT_VER}\")")
set(${ARGV0}_FOUND TRUE)
endif()
endmacro()
note: for gflags you may need to "force" gflags_NAMESPACE also since gflags it's a source not a binary (cf glog issue ToDo)

Clion IDE C++ false positive on syntax error

I am using Clion as IDE
I just updated my fedora from 24 to 25 in order to use C++17,
But unfortunately now, CLion (v 2017.2.3) display code error that wasn't there before the OS upgrade
Here an "error" shown using template argument as template for std::optional
Here an "error" shown using std::make_shared, saying I am using too many argument (I should need 2 apparently) while the actual constructor takes 3 argument. I remind that the code compile and works
My CMake file (dunno if it could help for this)
Cmake_minimum_required(VERSION 3.3)
project(FreeSouls CXX)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lprotobuf -lboost_timer -lboost_system -lboost_thread")
set(CMAKE_CXX_STANDARD 17)
if ( UNIX )
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Wextra -Wno-deprecated-declarations")
message( STATUS Set Warnings flag : ${CMAKE_CXX_FLAGS})
endif()
enable_testing()
add_subdirectory( Protobuff )
include_directories(
cmake-build-debug/Protobuff
FySMQ/include/queue
FySMQ/include/bus
FySMemoryManager/include
Server/include/gateway
Server/include/network
Server/include/babble
Server/include/game Utils/src)
set(SOURCE_FILES_MM)
set(SOURCE_PROTOBUF
cmake-build-debug/Protobuff/FySBabbleMessage.pb.cc
cmake-build-debug/Protobuff/FySGtwMessage.pb.cc
cmake-build-debug/Protobuff/FySLoginMessage.pb.cc
cmake-build-debug/Protobuff/FySAuthenticationResponse.pb.cc
)
set(SOURCE_FILES_MQ
FySMQ/include/queue/LockFreeQueue.hh
FySMQ/include/queue/QueueContainer.hh
FySMQ/include/queue/LockingQueue.hh
FySMQ/include/bus/BusListener.hh
FySMQ/include/bus/FysBus.hh)
set(SOURCE_FILES_SRV
Server/src/gateway/Gateway.cpp
Server/include/gateway/Gateway.hh
Server/src/gateway/Context.cpp
Server/include/gateway/Context.hh
Server/src/network/SessionManager.cpp
Server/include/network/SessionManager.hh
Server/src/babble/Babble.cpp
Server/include/babble/Babble.hh
Server/src/babble/BabbleChannel.cpp
Server/include/babble/BabbleChannel.hh
Server/src/network/TcpConnection.cpp
Server/include/network/TcpConnection.hh
Server/src/gateway/GameServerInstance.cpp
Server/include/gateway/GameServerInstance.hh
Server/src/gateway/Authenticator.cpp
Server/include/gateway/Authenticator.hh
)
set(SOURCE_FILES_UTILS Utils/src/CheckVars.hh Utils/src/TokenGenerator.cpp Utils/src/TokenGenerator.hh)
set(SOURCE_FILES_ALL
${SOURCE_PROTOBUF}
${SOURCE_FILES_UTILS}
${SOURCE_FILES_MQ}
${SOURCE_FILES_MM}
${SOURCE_FILES_SRV})
set(SOURCE_FILES
${SOURCE_FILES_ALL}
Server/src/main.cpp)
set(SOURCE_FILES_TEST
${SOURCE_FILES_ALL}
FySMQ/test/TestUnitMQ.cpp
Utils/test/TestCheckVar.cpp
FySMQ/test/FysBusTest.hh)
### Server ###
add_executable(FreeSouls ${SOURCE_FILES})
target_link_libraries(FreeSouls proto )
# link against dynamic libraries
add_definitions(-DBOOST_ALL_DYN_LINK)
## Test executable
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(UnitTests ${SOURCE_FILES_TEST})
target_link_libraries(UnitTests ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
add_test(UnitTests UnitTests)
Does this error ever happened to you? How can I fix that?
I found a post about a problem similar here
Linux CLion can't resolve namespace member
But it looked like it were a bug of the Clion and a new version fixed it,
knowing I was using the exact same version on fedora 24 and it was working fine before the upgrade, I don't know if it is the same.
As said by Chris, this was a bug from the CLion parser.
The release of the version 2017.3 has solved those issues.

Cmakelist working outside of Clion

I've wanted to use Clion for awhile but I've always had trouble with Cmake. Armed with Cygwin, I've almost gotten this stupid thing to work.
The issue is while I can compile a cmake file from within a cygwin terminal, in Clion I am told it cannot find the library I want.
Error:A required package was not found
The cmakelist.txt file
cmake_minimum_required(VERSION 3.3)
project(Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PKG_CONFIG_PATH /usr/lib/pkgconfig)
set(PKG_CONFIG_EXECUTABLE /usr/bin/pkg-config.exe)
set(SOURCE_FILES main.cpp)
add_executable(Test ${SOURCE_FILES})
INCLUDE(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED "sdl2")
MESSAGE(STATUS "SDL library: " ${SDL2_LDFLAGS})
TARGET_LINK_LIBRARIES(Test ${SDL2_LDFLAGS})
I have no idea if setting the variables PKG_CONFIG_PATH and others work, but they successfully build a makefile for my use in cygwin that builds correctly.
I've deleted the cache, remade the project and everything. It just refuses to work in Clion
If I understood correctly, your cmake config is unable to find SDL library. I found it better to use find_package command instead of pkg_check_modules.
In order to find_package(SDL2) to work, there must be FindSDL2.cmake module in directory, specified by CMAKE_MODULE_PATH variable (usually, it is cmake/Modules directory inside your source tree).
FindSDL2.cmake is not a part of CMake, but you can find one online easily (check my own modules, for example: https://github.com/dragn/cmake-modules).
Refer to this doc for details: https://cmake.org/Wiki/CMake:How_To_Find_Libraries.
Put FindSDL2.cmake to cmake/Modules directory and add this to your CMakeLists.txt:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
...
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
NOTE: Sadly, it appears that Leonardo has not succeeded in finding volunteers for maintaining FindSDL2.cmake in SDL community: https://cmake.org/Bug/view.php?id=14826.

Issue finding and linking Psapi library to executable using cmake

I am trying to link the psapi library to a project with cmake, nothing complex. Here's my cmake-file:
cmake_minimum_required(VERSION 2.8)
project(BenchmarkTests)
add_definitions(-DPSAPI_VERSION=1)
if (WIN32)
FILE(GLOB win32_head
Timer.h
win_Memory.h
win_Processor.h
BenchmarkTests.h)
FILE(GLOB win32_source *.cpp)
SET(win32_test ${win32_head} ${win32_source})
SET(LIBDIR_NAME "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64/")
SET(LIBDIR $ENV{${LIBDIR_NAME}})
SET(LIBNAME "Psapi.Lib")
find_library (Psapi ${LIBNAME} {LIBDIR})
ADD_EXECUTABLE(bmTests ${win32_test})
TARGET_LINK_LIBRARIES(bmTests Psapi)
SOURCE_GROUP("win32" FILES ${win32_test})
endif()
There are no other "Psapi.Lib" files on my computer except for in ".../um/x86", but my system is 64-bit so I want the x64, no? Anyhow the output in CMake GUI for Psapi field is "Psapi-NOTFOUND" and in VS2013 all of the functions in Psapi.h recieve syntax errors. I guess since they can't link to the library. Am I forgetting something vital in my cmake file? Any suggested fix or alternative method is welcome, thanks in advance.
I get the same result when I try the below instead of find_library(...)
add_library(Psapi STATIC IMPORTED)
set_property(TARGET Psapi PROPERTY IMPORTED_LOCATION "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64/Psapi.Lib")
For future reference I got it to work in CMake as follows, credit goes to Chibueze Opata on this question:
find_library (PSAPI Psapi)
...
add_executable(...)
...
target_link_libraries(Basic -lpsapi)