0

I have this code in CMake to find debug and release libraries for a project that I have:

FIND_LIBRARY(MP4V2_LIBRARY_RELEASE libmp4v2 HINTS "${MP4V2_DIR}/bin/Windows-x64/Release Static (MT)")
FIND_LIBRARY(MP4V2_LIBRARY_BEDUG libmp4v2 HINTS "${MP4V2_DIR}/bin/Windows-x64/Debug Static (MTd)")
set(MP4V2_LIBRARIES "optimized ${MP4V2_LIBRARY_RELEASE} debug  ${MP4V2_LIBRARY_BEDUG}") 
message(STATUS ${MP4V2_LIBRARIES})

and it is expanded correctly when I run CMake:

optimized D:/MyData/SourceCode/camm_mp4v2/bin/Windows-x64/Release Static (MT)/libmp4v2.lib debug  D:/MyData/SourceCode/camm_mp4v2/bin/Windows-x64/Debug Static (MTd)/libmp4v2.lib

and I added it to my application like this:

target_link_libraries(MyApp ${MP4V2_LIBRARIES})

When I create project for VS and try to compile it, I am getting this error:

cannot open file 'optimized D:\MyData\SourceCode\camm_mp4v2\bin\Windows-x64\Release Static (MT)\libmp4v2.lib debug  D:\MyData\SourceCode\camm_mp4v2\bin\Windows-x64\Debug Static (MTd)\libmp4v2.lib.lib'

Apparently, the optimized and debug library was not detected by CMake.

What is wrong with this code and how I can fix it?

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    You want to assign to variable `MP4V2_LIBRARIES` a **list** of `optimized`, `${MP4V2_LIBRARY_RELEASE}`, `debug` and `${MP4V2_LIBRARY_BEDUG}` but enclose this list into **double quotes**... So, instead of the list, the value of the variable is just a **string** from 4 **space**-separated words. Exactly this you observe in `message()` output. Either remove double quotes or separate words with semicolon `;`. – Tsyvarev Apr 16 '20 at 10:40
  • @Tsyvarev This solved my problem. Please raised it as an answer and I will accept it. – mans Apr 17 '20 at 11:11

1 Answers1

1

Create an IMPORTED target instead:

add_library(mp4v2 STATIC IMPORTED)
set_target_properties(mp4v2 PROPERTIES
    IMPORTED_LOCATION_DEBUG ${MP4V2_LIBRARY_DEBUG}
    IMPORTED_LOCATION_RELEASE ${MP4V2_LIBRARY_RELEASE})
target_link_libraries(MyApp mp4v2)

If there are any headers you can set the INTERFACE_INCLUDE_DIRECTORIES property as well.

Botje
  • 26,269
  • 3
  • 31
  • 41
  • Thanks, can you please elaborate? Why my code doesn't work? I can not change the line "Target_Link_Libraries" as there are several other targets (Linux and so on) that they prepare ${MP4V2_LIBRARIES} and I cannot change them. – mans Apr 16 '20 at 10:02
  • The "optimized" and "debug" keywords are meaningless to CMake. – Botje Apr 16 '20 at 10:02
  • I learned from this answer: https://stackoverflow.com/questions/2209929/linking-different-libraries-for-debug-and-release-builds-in-cmake-on-windows – mans Apr 16 '20 at 10:05
  • Ah, I stand corrected. @Tsyvarev's answer is correct then. – Botje Apr 16 '20 at 11:08