0

I'm trying to set automatically run/debug environmental variables for my project in Visual Studio. I mean, is there any CMake or C++ code line to do this not needing to do it manually?

Here are the instructions how to do it manually (what I want to avoid).

Here there is an still unsolved question about how to do it with Cmake (seems not to be possible).

I also tried with setenv() and putenv() in different ways but it didn't work, because the main function doesn't even run until that line of code, before an error message shows up: "Some.dll was not found" and the program stops.

MiguelVega
  • 13
  • 3
  • Hello and welcome to StackOverflow. Please take some time to read the [help page](https://stackoverflow.com/help), especially the sections named [How to Ask](https://stackoverflow.com/help/how-to-ask). You might also want to learn about [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and how to [format your question](https://stackoverflow.com/editing-help). Do you want a run time environment variable or a compile time define? – Tarick Welling Jun 19 '19 at 10:23
  • You may be able to edit the Visual Studio `.user` file with commands in CMake. https://cmake.org/cmake/help/latest/command/file.html It's just xml. – drescherjm Jun 19 '19 at 11:47

2 Answers2

0

If your dll is one you are intending to use, this answer details how to quickly ensure it is found at runtime (putting the DLL alongside the executable)

If by 'automatic' you mean in code, you can set environment variables in code using _putenv as described in this answer similar to what you seem to be describing.

ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());
snik
  • 1
  • 2
  • Actually putting the DLLs alongside the executable works fine, but I would like to set the environment variables (specifically the PATH variable) before building my project i.e. before the creation of the executable. I think, maybe It could be done by the configuration of the CMake.txt file, but I still don't know how. – MiguelVega Jun 20 '19 at 21:55
  • Re: [https://stackoverflow.com/questions/7584602](this question), you might have to set the path var before calling cmake, rather than from within it. Unless you're manually loading your dlls with LoadLibrary, you'll be restricted on where dlls are automatically searched for (eg if they're in a sub directory), see [https://stackoverflow.com/a/35311436/7708798](this answer) for more information. – snik Jun 23 '19 at 13:34
0

The solution I found is base on this answer.

Steps for the solution:

  1. Create a UserTemplate.vcxproj.user file next to the CMakeLists.txt file, with the next content:
<?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <LocalDebuggerEnvironment>PATH=..\Your\Path\to\Binaries;%PATH%".</LocalDebuggerEnvironment>
        <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
      </PropertyGroup>
    </Project>

Where ..\Your\Path\to\Binaries is the relative path to your binary files (the two points at the beginning .. are optional, if you want to go up in the relative directory path, you may want to use them).

  1. Add the next lines of code in the CMakeLists.txt file.
    # Configure the template file
    SET(USER_FILE main.vcxproj.user)
    SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})
    CONFIGURE_FILE(UserTemplate.vcxproj.user ${USER_FILE} @ONLY)

Where ProjectName is the name of the VS project where you want to define your PATH variable.

MiguelVega
  • 13
  • 3