简体   繁体   中英

CMake - Define dlls directory or files for a generated project

I have a project that I am building using CMake for Visual Studio .
The .exe file requires 2 dll files (That I am copying to the Debug forlder for now).

Is there a way to add the dlls / dlls directory through CMakeLists.txt / FindLibrary.cmake
(the same way it's done with find_library to find *.lib or in some other way that I ignore) so that I don't copy them MANUALLY inside the Debug folder each time I generate the project in another folder/pc (since the dll's folder is known)?

UPDATE :

CMakeLists.txt

..
..
set (ENVLIB $ENV{MYLIB})
FUNCTION (CONFIGURE_DEBUGGER TARGET_NAME)
  CONFIGURE_FILE(common/build/template/Main.vcxproj.user
    ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.vcxproj.user
    @ONLY
    )
ENDFUNCTION (CONFIGURE_DEBUGGER)
..
..
ADD_EXECUTABLE(Main Main.cxx)
CONFIGURE_DEBUGGER(Main)

Main.vcxproj.user

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments>
    <LocalDebuggerEnvironment>PATH=@ENVLIB@bin;$(Path)
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
  </PropertyGroup>

  <!-- Additional PropertyGroups for the rest of Configuration/Platform combos -->

</Project>

Output of vcxproj.user after generation
<LocalDebuggerEnvironment>PATH=C:\\Program Files\\MyLib\\bin;$(Path)

The variable ENVLIB has been changed to the correct PATH when the file has been copied but
Visual studio is still asking for those DLLs. It's like it is ignoring the file .vcxproj.user

SOLVED :

Solved changed the property : <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> to
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

First option would be to create a custom target, that will run a CMake script to copy the files.

The second option (which I prefer) is to use CONFIGURE_FILE to generate a .vcxproj.user file, which set up LocalDebuggerEnvironment such that the directory with the DLLs is added to the PATH.

For example, my build system defines a function CONFIGURE_DEBUGGER :

FUNCTION(CONFIGURE_DEBUGGER TARGET_NAME)
  CONFIGURE_FILE(${ROOT}/common/build/template/executable_vs14.vcxproj.user
    ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.vcxproj.user
    @ONLY
    )
ENDFUNCTION(CONFIGURE_DEBUGGER)

I call this function right after I define an executable target, eg

ADD_EXECUTABLE(example
  ${EXAMPLE__SRC}
  ${EXAMPLE__HDR}
)
CONFIGURE_DEBUGGER(example)

The template executable_vs14.vcxproj.user looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments>
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory>
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path)
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
  </PropertyGroup>

  <!-- Additional PropertyGroups for the rest of Configuration/Platform combos -->

</Project>

Note: In the above example, I also set some default command arguments to run the app with when debugging as well as the working directory where it runs -- adjust things as you need.

Note 2: Now that I'm looking at it, perhaps we can change the Condition on the PropertyGroup to make it apply to every configuration/platform. Gotta look into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM