7

How can I set with CMake in VisualStudio2010 the property "Additional Library Directories".

Example:

%(AdditionalLibraryDiretories) = "d:/librarys/wnt/i386/debug/"

enter image description here

configuration parameter -> linker -> general-> "Additional Library Directories"

I tried this and it doesn't work.

link_directories("d:/librarys/wnt/i386/debug/")
post4dirk
  • 313
  • 4
  • 16
  • I just tested it with VS2012 / CMake 3.3.0 and it seems to work fine. If you put `link_directories("d:/librarys/wnt/i386")` before my `add_executable(...)` call I get a "Additional Library Directories" property with `d:/librarys/wnt/i386;d:/librarys/wnt/i386/$(Configuration);%(AdditionalLibraryDirectories)`. See [`link_directories()`](https://cmake.org/cmake/help/v3.3/command/link_directories.html) documentation. – Florian Oct 21 '15 at 11:11
  • 1
    THX Florian it works. My problem was that **add_execute** before **link_directory** – post4dirk Oct 21 '15 at 11:49
  • The problem now is that **AdditionalLibraryD‌​irectories** depends on the Configuration. There is an other path for **debug** libraries than **release** libraries – post4dirk Oct 21 '15 at 11:54
  • Please see my last comment. I removed your `debug` part there, because CMake does - to cover libraries depending on Config - include two variants of what you have given in `link_directories()`: `d:/librarys/wnt/i386` and `d:/librarys/wnt/i386/$(Configuration)`. And just a hint: I never needed `link_directories()` in any of my projects. So I'm wondering - admittedly without knowing your use case - if you could not use [`target_link_libraries()`](https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html) instead? – Florian Oct 21 '15 at 13:14
  • The pathes doesn't named **d:/librarys/wnt/i386/ $(Configuration) = debug** instead e.g. **d:/librarys/wnt/i386/fooba**. – post4dirk Oct 21 '15 at 13:30
  • That's the point. `link_directories()` has a very limited scope with a dedicated set of functionality, so I would do something like `target_link_libraries(MyExe debug "d:/librarys/wnt/i386/fooba/foo.lib")`. But if you really want to force it I can only think of extending the linker flags directly: call `set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /LIBPATH:\"d:/librarys/wnt/i386/fooba\"")` after your `project(...)` command. Then I get `%(AdditionalLibraryDirectories);d:/librarys/wnt/i386/fooba`. – Florian Oct 21 '15 at 13:56

1 Answers1

8

Turning my comments into an answer

What does link_directories() cover?

I tested it with VS2012 / CMake 3.3.0 and if you put your link_directories(...) before your add_executable(...) call it seems to work fine.

link_directories("d:/librarys/wnt/i386")

get_directory_property(_my_link_dirs LINK_DIRECTORIES)
message(STATUS "_my_link_dirs = ${_my_link_dirs}") 

add_executable(...)

Everything you add with link_directories() will be appended to the directory property LINK_DIRECTORIES and assigned to whatever targets are listed afterwards.

In the upper example I get in Visual Studio "Additional Library Directories" property:

d:/librarys/wnt/i386;d:/librarys/wnt/i386/$(Configuration);%(AdditionalLibraryD‌​irectories)

CMake does - to cover libraries depending on Config - include two variants of what you have given in link_directories(): d:/librarys/wnt/i386 and d:/librarys/wnt/i386/$(Configuration).

What if you need more flexiblity?

If your debug/release path names are not matching the VS configuration name (e.g. fooba for debug), then you can't use link_directories(). One approach would be to extend the linker flags directly:

project(...)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /LIBPATH:\"d:/librarys/wnt/i386/fooba\"") 

Then I get in the Debug config properties:

%(AdditionalLibraryDirectories);d:/librarys/wnt/i386/fooba

For the lack of flexibility of link_directories() I normally only use the target_link_libraries() command. E.g.:

target_link_libraries(MyExe debug "d:/librarys/wnt/i386/fooba/foo.lib")

would give in the Debug "Additional Dependencies" property:

kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib;d:\librarys\wnt\i386\fooba\foo.lib

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149