2016-04-24 27 views
1

我正在使用CMake生成一个可执行文件和多个库。我已将可执行文件的输出文件夹和所有库指定为一个常见的“bin”文件夹。如何使用相对路径在CMake生成的解决方案中打开源代码中的文件

使用:

set_target_properties (${PROJ} 
    PROPERTIES 
    ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_DIR}" 
    ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${BIN_DIR}" 
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_DIR}" 
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BIN_DIR}" 
    LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_DIR}" 
    LIBRARY_OUTPUT_DIRECTORY_RELEASE "${BIN_DIR}" 
) 

在图书馆之一,我使用的是相对路径打开一个资源文件。此路径相对于“bin”文件夹.//Resources//file.jpg(因为Resources文件夹位于bin文件夹中)。

当我从Visual Studio调试器运行exe时,发现它正确运行bin文件夹中的exe文件,但是当它试图打开file.jpg时,它在相对于CMAKE_BINARY_DIR的路径中查找。所以,CMAKE_BINARY_DIR/Resources/file.jpg。所以,我得到一个运行时错误,说没有找到文件。

解决此问题的最佳方法是什么?我不想复制CMAKE_BINARY_DIR中的所有资源。似乎应该有更好的方法。

感谢您提前提供任何帮助。

+1

您需要设置调试正确的工作目录中的Visual Studio项目属性。它应该是'$(TargetDir)'而不是默认的'$(ProjectDir)'。 –

+0

谢谢丹。那就是诀窍。我接下来的问题是如何在cmake中设置它,这样你就不必通过VS来设置它。但显然,没有简单的方法来做到这一点,它必须通过修改.user文件来完成。您可以发布您的答案作为答案。 – ak22

回答

0

要解决您的问题,您需要在Visual Studio项目属性中设置用于调试的正确工作目录。它应该是$(TargetDir)而不是默认的$(ProjectDir)

Properties Dialog


为了避免每次你生成一个干净的解决方案时手动修改设置,您可以使用CONFIGURE_FILE预先生成一个默认。用户文件。

例如,我用我的VS 2013项目下面的模板设置:

  • 默认的命令行参数调试器中运行应用程序时使用
  • 工作目录使用(所有的配置共享一个)
  • 调整PATH包括目录与所有的预构建的依赖性和符号

文件${ROOT}/build/template/executable_vs12.vcxproj.user

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="12.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> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|x64'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|x64'"> 
    <LocalDebuggerCommandArguments>-D20</LocalDebuggerCommandArguments> 
    <LocalDebuggerWorkingDirectory>$(TargetDir)\..\..\common\</LocalDebuggerWorkingDirectory> 
    <LocalDebuggerEnvironment>PATH=$(SolutionDir)..\deps\bin;$(Path) 
$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment> 
    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> 
    </PropertyGroup> 
</Project> 

接下来,我有一个可以包含并包装配置步骤简单.cmake文件。

文件${ROOT}/build/debugger_config.cmake

IF(NOT COMMON_BUILD_DEBUGGER_CONFIG_INCLUDED) 
SET(COMMON_BUILD_DEBUGGER_CONFIG_INCLUDED TRUE) 
# ============================================================================= 
FUNCTION(CONFIGURE_DEBUGGER TARGET_NAME) 
    CONFIGURE_FILE(${ROOT}/build/template/executable_vs12.vcxproj.user 
    ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.vcxproj.user 
    @ONLY 
    ) 
ENDFUNCTION(CONFIGURE_DEBUGGER) 
# ============================================================================= 
ENDIF(NOT COMMON_BUILD_DEBUGGER_CONFIG_INCLUDED) 

最后,我调用这个函数的每个可执行的目标定义后:

# This can go in top-level CMakeLists.txt 
INCLUDE(${ROOT}/build/debugger_config.cmake) 

# ... 

ADD_EXECUTABLE(foo 
    ${FOO_FILES} 
) 
CONFIGURE_DEBUGGER(foo) 
相关问题