qPolak
qPolak

Reputation: 43

Linking math library in CMake file on windows and linux

I have been able to write a CMakeLists.txt file that is able to build my C project on Linux, however, I have been having a lot of trouble to build the project on windows. The cmake .. call succeeds, and Visual Studio 2017 project files are generated, but the build then fails siting: Error LNK1104 cannot open file 'm.lib'. In the CMakeLists.txt file I am using target_link_libraries(MY_EXECUTABLE m) to try and link the math library, which works on linux, but the above error occurs on windows. After some research, it seems to me that math is handled by the mscvr library on windows, not libm as on linux, but I'm not sure how to configure the CMake file so that I can build on both operating system.

Does anyone have an idea on how I could set this up to be able to build in both environments?

Upvotes: 4

Views: 2859

Answers (1)

John Bollinger
John Bollinger

Reputation: 180161

Visual Studio does not need or want you to explicitly request linking the math library. You must avoid adding it as a link library when building for Windows. Instead of unconditionally doing target_link_libraries(MY_EXECUTABLE m), then, you might use:

IF (NOT WIN32)
  target_link_libraries(MY_EXECUTABLE m)
ENDIF()

Upvotes: 2

Related Questions