Setting a VS2010 environment variable from a batch file? - c++

I'm using a batch file to define some variables that will be used in my program.
I want the batch file to change the environment variable and use it in my code , but it's just now working - the macro is not being changed.
to be more specific and clear :
I have a program that creates a DLL and sets it's version
In the common setting of the project - I created a new macro (Common properties->User macros) : TEST_VER = 5
now I want to add a batch file , that will run in the pre-build command and change the value of TESTER
I wrote this in the batch file:
set TEST_VER=9
and used the path of the batch in the pre-build.
BUT it doesn't recognize it.
and still uses 5 as the value
I though doing :
propeties of the project - > resourcses ->general
and add : TEST_VER=$(TEST_VER)
and still didn't work
is there a way to do it??
thanks!!

When Visual Studio starts a program, it runs that program in a new sub-process. In this case, that's a new CMD.EXE, the command prompt shell. Changes made to the environment in a sub-process, a child, have no effect on the parent. Visual Studio has its own set of environment variables which it inherited when it started. Your batch file can't change those values. You can't do what you want the way you're doing it.

Related

Setting environment variable for VS2017

I'm installing a software extension for VS2017 which requires me to create a batch file in order to set the 'TargetVisualStudioEdition' environment variable, in my case the variable should be set to Community.
What I did so far is to crate the following simple batch file:
set "%TargetVisualStudioEdition%"="Community"
Once I run it I receive:
set ""="Community"
Then, when I run the installer the installation stops because the environment variable has not been set.
Am I missing something?
don't think you need the %s wrapping the variable;
just do set VARIABLE = value
the %% is just to reference the variables.
if this variable will be shared among processes, use setx

Change Eclipse Dynamic Variable to build a single c++ class

I would like to change the dynamic variable ${selected_resource_loc} such that I get rid of the extension (and can add another one).
The purpose is to build the current selected file, but therefore I don't need the .cc-file in my command but the .o-file with the same name.
Detailed description:
I have a customized make command (lets say make) and would like to execute make selectedFileName.o, where the current selected file in eclipse is selectedFileName.cc. (I put the command as build command in project properties -> C/C++ Build.) With variable ${selected_resource_loc} in behaviour tab I get make selectedFileName.cc.
I solved my problem by writing a skript where I modify the ${selected_resource_loc} and included it via external tools in eclipse.

execute c-programfiles from CMD in windows

I have recently installed MinGW to my computer, to compile and run programs written in c.
Right now I have to manually go to the bin-folder to execute and compile files.
The path is C:\MinGW\bin
Is there a a way to avoid this everytime? I want be able to directly write the commands when I open the command Line.
I tried to follow the Environment Settings on http://www.mingw.org/wiki/Getting_Started
but it does not work at all.
A simple solution would be a small batch script. Create a new batch file with this code:
#echo off
cd C:\MinGW\bin
:loop
set /p var=
%var%
goto loop
Could you show me your user environment variable called "path"? Maybe we will find the error there.
Edit:
Create a new environment variable in the upper field (user-environment variables). Enter this in the window which appears.
Name: PATH
Value: C:\MinGW\bin
This should work.

Compiling a .cpp file from the body of another .cpp file

I've been working on an application that compiles raw .cpp files and analyzes their outputs, using Microsoft Visual Studio 2010 Command Prompt. I'm having a lot of trouble, and there doesn't seem to be much material about this online. Here's the troublesome code:
#include <iostream>
using namespace std;
...
string name = "cl /EHsc ";
name += "example.cpp";
system("setupcppenv.bat"); // A short batch file I wrote to launch the VC++ cmd prompt without launching another instance of cmd
system(name.c_str());
When I execute (it attempts to compile example.cpp), I get an error:
fatal error C1043: iostream: no include path set
I'm not very experienced with batch files, or using the command prompt compiler. What am I doing wrong?!
Additionally, is there a different way to compile from inside an application?
Thanks!
Each system() call invokes a separate process, so any environment variables you set in your setupcppenv.bat file will be discarded once that process ends.
What you should do instead, is to add the environment variables you are setting in your .bat file to the system environment, or at least to the environment of the cmd instance from where you launch your application, so that they are inherited by the process started by the system() call.
I don't know what's in setupcppenv.bat I would guess that you're making changes to environment variables in that batch file. What happens is that when the batch script ends, those environment variable changes are being lost becuase they're confined to the batch script's process and any children of that process.
A way to set environment variables that will work is to use the setenv() or putenv() functions in your program.

How can I persist an environment variable to the visual studio build helper?

I've got an NMake project in Visual Studio 2008 that has the Build command set to a custom tool that performs a long build task.
Build = "#call MyTool -config $(ConfigurationName)"
I want a way to to pass a special flag ("-quickbuild") to my tool to tell it to do a quick subset of the overall build.
Build = "#call MyTool -config $(ConfigurationName) -quickbuild"
However I want it to be easy to switch between them so I don't actually want to change the build command.
My thought was to change the build command to this:
Build = "#call MyTool -config $(ConfigurationName) $(ShouldQuickbuild)"
and create a visual studio macro that will set the "ShouldQuickbuild" environment variable to "-quickbuild" then call DTE.Solution.SolutionBuild.BuildProject(...) on the project. The problem is it doesn't see the "ShouldQuickbuild" environment variable.
Any ideas on how I can get this working. Is there a better approach for doing what I want?
Use a batch file and check, If the environment is passed on to the batch file, then you can get that in the batch file and call the actual tool that you want.
The batch file would look like this :
#echo off
MyTool -config %1 %ShouldQuickbuild%
IF the environment is not passed to the batch file, you have to somehow get the info across, globally. Is it possible to create a file from a VS macro? Or call an EXE? Then it's quite simple..
Try putting your variable inside of % delimiters, as in %ShouldQuickBuild%.
You can you control this with the "Solution Configureation". Create two new configureations "Debug Quick" and "Release Quick". These would be copies of the originals. Then change the build command for each configuration.