0

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")
Daniel N
  • 53
  • 1
  • 11
  • 1
    Use "Visual Studio 12 2013 Win64" generator for configure x64 project. Path in `find_library` should come with `PATH` or `HINT` option, e.g. like this: `find_library(Psapi Psapi.Lib PATH "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64/")`. Note, that `find_library` sets *variable*, not a *target*, so you need to dereference this variable for use library: `target_link_libraries(bmTests ${Psapi})`. – Tsyvarev Dec 10 '15 at 07:52
  • Thank you, did as you asked but still no luck, the only difference now is that cmake doesn't complete configuration because "following variables are used in this project, but they are set to NOTFOUND", which is to be expected. By the way aren't static libraries supposed to end with .a or .lib? Does .Lib signify something else or screw with Cmake in any way? I thought file extensions were case-sensitive. – Daniel N Dec 10 '15 at 13:12
  • 1
    Static libraries on Windows use `.lib` extension (unless `MinGW` in question, but it is not your case). When path to library is already known, approach with IMPORTED library is better than using `find_library()`. What precise problem(error log) is with your second approach? Note, that `all of the functions in Psapi.h recieve syntax errors` has nothing common with library file. **Syntax** error signals about problem in source or header file. – Tsyvarev Dec 10 '15 at 13:27
  • CMake does not produce any errors with the second approach. The errors arise from attempting to build the generated project in VS, these are the syntax errors I was talking about. And I am also getting syntax errors in other std headers I'm including in my project. You're right, this is a problem in my project files. Thanks for helping clear that up. – Daniel N Dec 10 '15 at 13:50

1 Answers1

2

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)

Halcyon
  • 1,376
  • 1
  • 15
  • 22