Issue finding and linking Psapi library to executable using cmake - 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)

Related

Not able to link GLEW to a project using CMake

May someone recommend me, how could I link my project to the GLEW via cmake.
For instance, I did well for the GLFW, according to its documentation:
...
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_SOURCE_DIR}/application/dependencies/GLFW)
...
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_MAIN})
target_link_libraries(${PROJECT_NAME} glfw)
I haven't found the good documentation by GLEW. I also would note I've installed this version of the GLEW, where CMakeLists.txt located in the root directory. I've tried different options, however the project not capable to find the glwe lib.
My best attempt looks like:
add_subdirectory(${CMAKE_SOURCE_DIR}/application/dependencies/GLEW)
...
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_MAIN})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/application/dependencies/GLEW/include)
target_link_libraries(${PROJECT_NAME} glfw GLEW)
But, it seems foolish. I mean if I properly comprehend, glew's CMake generates static/dynamic lib, and at the next step I link one to the project. However, it's still not able to detect glew lib.
There is no CMake target called GLEW in the linked project. The library targets are called libglew_static and libglew_shared. You should be using the library targets as defined in the glew project CMakeLists.txt.

CMake Add own library to project using find_library()

I am currently creating a set of classes that should be used in
different projects using CLion. My question is how do I implement this
functionality. So far I looked into the following related issues
which didn't really solve my problem:
CMake link to external library
Add external libraries to CMakeList.txt c++
CMake reference for add_library(), find_library() which I don't fully understand yet since I am fairly new to CMake
I created two sample projects "TestLib" and "TestProj":
TestLib src Class.h Class.cpp
CMakeList.txt TestProj main.cpp
CMakeList.txt
The CMakeList.txt for "TestLib" currently looks as follows:
cmake_minimum_required(VERSION 3.5)
project(TestLib)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
set(SOURCE_FILES src/Class.cpp src/Class.h)
add_library(TestLib ${SOURCE_FILES})>
Now, I tried to use this library in "TestProj" using the following
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(TestProj)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
find_library(CLASS_LIB TestLib HINTS /home/user/.CLion2016.1/system/cmake/generated/TestLib-7507f101/7507f101/Debug)
set(SOURCE_FILES main.cpp)
add_executable(TestProj ${SOURCE_FILES})
target_link_libraries(TestProj CLASS_LIB)
CMake finds the library but
I dont have access to Class.h of the library
Writing the whole /home/user/.CLion2016.1/...-Path to the library seems to be wrong
Any help is really appreciated. Thank you.
If the goal is to make your library available to multiple other projects, the recommended way to do that would be to have your library project's install procedure generate CMake configuration files. Unfortunately, the procedure to do that is a bit arcane, but https://cmake.org/cmake/help/git-master/manual/cmake-packages.7.html#creating-packages should give you a starting point on how to do this.
You can put at the root of your projects CMakeLists.txt
with such content:
add_subdirectory(TestLib)
add_subdirectory(TestProj)
after that you can write in TestProj's CMakeLists.txt just
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../TestLib)
target_link_libraries(TestProj TestList)
and no need to use find_library

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.

Cross Compiling problems with nestled CMakelists.txt files

I do need to include "Cross Compiling" in my project (Intel and PowerPC) and I have found a problem defining targets. I have the following folder structure:
libs
*.h
*.cpp
CMakeLists.txt
src
*.h
*.cpp
CMakeLists.txt
...
-CMakeLists.txt
If a write the following piece of code in the CMakeLists.txt of the src folder, It works ...
ADD_EXECUTABLE( intel ${SOURCES} ) SET_TARGET_PROPERTIES( intel PROPERTIES OUTPUT_NAME ${APP_NAME}_INTEL EXCLUDE_FROM_ALL true ) TARGET_LINK_LIBRARIES( intel ${LIBS_INTEL} )
However I don't really know how to do it in the CMakeLists of the libraries (libs folder). If I follow the same structure and use "intel" as a target, it returns an error message saying that "the target name is already used".
I do want to compile all my project with both options:
"make intel" --> to generate Intel version and "make powerPc" --> to generate PowerPC version
I have been looking for it on the Internet but I couldn't find a correct solution.
I hope anyone could help me.
Finally I found the solution. It was as easy as using the following macro in the main CMakeLists.txt file.
ADD_CUSTOM_TARGET ( INTEL DEPENDS EXECUTABLE1 EXECUTABLE2 ... EXECUTABLEN)
And the same with the other target powerpc.

How do I tell CMake to link in a static library in the source directory?

I have a small project with a Makefile which I'm trying to convert to CMake, mostly just to get experience with CMake. For purposes of this example, the project contains a source file (C++, though I don't think the language is particularly relevant) and a static library file which I've copied from elsewhere. Assume for argument's sake that the source code to the library is unavailable; I only have the .a file and the corresponding header.
My handmade Makefile contains this build rule:
main: main.o libbingitup.a
g++ -o main main.o libbingitup.a
which works fine. How do I tell CMake to reproduce this? Not literally this exact makefile, of course, but something that includes an equivalent linking command. I've tried the obvious but naive ways, like
add_executable(main main.cpp libbingitup.a)
or
add_executable(main main.cpp)
target_link_libraries(main libbingitup.a)
as well as various things with link_directories(.) or add_library(bingitup STATIC IMPORTED) etc. but nothing so far that results in a successful linkage. What should I be doing?
Version details: CMake 2.8.7 on Linux (Kubuntu 12.04) with GCC 4.6.3
CMake favours passing the full path to link libraries, so assuming libbingitup.a is in ${CMAKE_SOURCE_DIR}, doing the following should succeed:
add_executable(main main.cpp)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a)
If you don't want to include the full path, you can do
add_executable(main main.cpp)
target_link_libraries(main bingitup)
bingitup is the same name you'd give a target if you create the static library in a CMake project:
add_library(bingitup STATIC bingitup.cpp)
CMake automatically adds the lib to the front and the .a at the end on Linux, and .lib at the end on Windows.
If the library is external, you might want to add the path to the library using
link_directories(/path/to/libraries/)
I found this helpful...
http://www.cmake.org/pipermail/cmake/2011-June/045222.html
From their example:
ADD_LIBRARY(boost_unit_test_framework STATIC IMPORTED)
SET_TARGET_PROPERTIES(boost_unit_test_framework PROPERTIES IMPORTED_LOCATION /usr/lib/libboost_unit_test_framework.a)
TARGET_LINK_LIBRARIES(mytarget A boost_unit_test_framework C)
I want to add to the other comments, the project name is the first argument. I had a project called cmakecompile and i wanted to add libusb to it, the code is as follows,
add_executable(cmakecompile main.c)
target_link_libraries(cmakecompile "D:/msys2/mingw64/lib/libusb-1.0.a")
the project had just only a main.c file, the first parameter in target_link_libraries is the name of your project and the second parameter is the path of the library.
Note that may help: Since i am compiling under windows, i had to install msys2 because the library you have has to be compiled with the same compiler. For example if you get libusb and try to use it in a qt-creator project, it will not work and you may get unreferenced functions, therefore i had to install msys2 and install libusb from inside msys2, install make and create a QT Cmake project and compile from Qt creator using the msys2 make.
The full cmakelists.txt is as follow
cmake_minimum_required(VERSION 3.5)
project(cmakecompile LANGUAGES C)
add_executable(cmakecompile main.c)
target_link_libraries(cmakecompile "D:/msys2/mingw64/lib/libusb-1.0.a")