How to use an eclipse variable for include path - eclipse-cdt

Using eclipse luna sr2 on 32 bit linux host.
For my cdt (managed make) project, I' defined some variables, so that in my .project were something like this:
<variableList>
<variable>
<name>Foundation</name>
<value>$%7BPARENT-3-PROJECT_LOC%7D/Framework</value>
</variable>
<variable>
<name>Cubehal</name>
<value>$%7BPARENT-3-PROJECT_LOC%7D/Framework/lib/Stm/STM32Cube_FW_F4_V1.9.0</value>
</variable>
<variable>
<name>Freertos</name>
<value>$%7BPARENT-3-PROJECT_LOC%7D/Framework/lib/FreeRtos/FreeRtos_8.2.2</value>
</variable>
</variableList>
(I e.g. use that for <linkedResources> …)
Now I also want to reuse this variables for the include paths (in .cproject) but fail when try to expand them. I tried to use ${env_var:…} like ${env_var:Foundation}/include but the ${env_var:Foundation} part always expands to an empty string (it works when using with an OS environment variable, but not with a variable defined in .project).
Any suggestion how to solve this?

Related

Set debug/run environment variable in Visual Studio 2017 C++ project?

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.
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());
The solution I found is base on this answer.
Steps for the solution:
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).
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.

Define user macros in different property sheet

Is it somehow possible to do following:
branch.prop:
// excerpt
<PropertyGroup Label="UserMacros">
<Branch>Trunk</Branch>
</PropertyGroup>
And another property sheet, common.prop:
// excerpt
<PropertyGroup>
<OutDir>D:\output\$(Branch)\VW$(VW_VERSION)\$(KN_BUILD)\</OutDir>
<IntDir>D:\output\$(Branch)\VS_Output\$(VW_VERSION)\$(KN_BUILD)\$(ProjectName)\</IntDir>
</PropertyGroup>
Problem
The problem is, that I use both property sheets in my project and if I show my projects properties I see that it shows the correct output directory and intermediate directory (like e.g. ' D:\output\Trunk\VW2016\Debug\' ) but when I compile my project it does not work, meaning that the user macro is missing so that logs show lines like following:
Library "D:\output\\VS_Output\2016\Debug\SomeProject\SomeProject.lib" ...
Pay attention to \\ instead of \Trunk\ in the log line!
Is there some way that property sheets work with user macros defined in different property sheets? Something like nesting them e.g. or any other trick?
I want to avoid to define my output directory and intermediate directory in each project manually (which would work)...
NOT WORKING ALTERNATIVE IDEAS
use a prebuild script that reads out the current svn path and setting an environment variable => because the environment variable is only read by VS on VS start, so if the prebuild script changes it VS does not recognise this until it is restarted
Why do I need this
I need this because I'm forced quite often to switch between trunk and a branch to create a hotfix and so want to avoid long build times because of overwriting intermediate / output files...
You can import your branch.prop in common.prop, like this:
<ImportGroup Label="PropertySheets" >
<Import Project="branch.prop"/>
</ImportGroup>

How to efficienlty get all include paths in MSVC2013

I'm tyring to refactor my code using include-what-you-use, but I need to figure out exactly which directories VS is looking in.
How do I get a listing of all include paths visible to files in my project?
If I add an incorrect include, something like "#include " I get the listing I want, but I can't copy and paste the damn thing! (and OCR doesn't seem to work).
How do I get this list?
Copy/pasting from the dialog is ok if you want to do this once manually but doesn't scale well. Since you tagged this msbuild, here's a solution using it. Create a file names showincludes.targets and paste this msbuild code:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ShowIncludes">
<Message Text="$(Include);%(ClCompile.AdditionalIncludeDirectories)"/>
</Target>
</Project>
Now to get the list of includes for any project file use
msbuild \path\to\vcxproj /p:ForceImportAfterCppTargets=\path\to\showincludes.targets /t:ShowIncludes
This will import said file into the project give, so it has access to all properties, and print out the value of the INCLUDE environment variable which is by default used by the compiler, and also the value of AdditionalIncludeDirectories which is passed to the compiler by msbuild.

Environment variables in VisualStudio?

Can I somehow define environment variables in VisualStudio?
Background:
I have two machines I am developing on, A and B.
On A, Boost is installed in "C:\boost"; on B, Boost is installed in "D:\boost".
I would like to somehow specify the include directory in the project configuration like
%(BoostDir)\include
and define BoostDir separately on each of the two machines.
You can read environment variables and use them. Microsoft has this documented:
<Project DefaultTargets="FakeBuild">
<PropertyGroup>
<FinalOutput>$(BIN_PATH)\myassembly.dll</FinalOutput>
<ToolsPath Condition=" '$(ToolsPath)' == '' ">
C:\Tools
</ToolsPath>
</PropertyGroup>
<Target Name="FakeBuild">
<Message Text="Building $(FinalOutput) using the tools at $(ToolsPath)..."/>
</Target>
</Project>
You can specify BIN_PATH either using a fixed environment variable that even survives reboots or set the value "on the fly" before calling Visual Studio:
#echo off
SET BIN_PATH=C:\Whatever
C:\Programs\VisualStudio\VS.exe
Take this as an example and adopt it to your needs.

Can I pass a preprocessor definition to the resource compiler through the command line?

I'm currently trying to switch between a few different default Icons in a Visual C++ .rc file using #ifdef tags.
The builds switching the #define value are being created through command line using MSBuild.
The difficulty I have been running into is that using Visual Studio 2010, in order to pass a preprocessor definition to the resource compiler you must define it in the project settings (Config Properties -> Resources -> General).
This makes it difficult to use an #ifdef tag because using this method it will always be defined in the resource compiler.
I would love to define it to a value, so that I might use a preprocessor #if SOMEVALUE == 4 might work, but Cannot seem to find a way to pass a Preprocessor definition + value to MSBuild via the command line.
Does anyone know a way to pass a preprocessor definition directly through to the resource compiler or a way to define a value for a preprocessor definition via commandline for msbuild?
Yes, this can be done.
Try using environment variables to pass values into your build process.
In your project properties add ;$(CMDLINE_DEFINES) to the end of your resource preprocessor definitions. (Be sure to pick the right configuration.)
Then when you use MSBuild from the command line type (or add to a batch file)...
C:\Projects\SomeProject> set CMDLINE_DEFINES=SOMETEST=42
C:\Projects\SomeProject> MSBuild SomeProject.vcproj
A batch file may look like:
#echo off
SET CMDLINE_DEFINES=%1
MSBUILD SomeProject.vcproj
Using this batch file, whatever you pass on the commandline will be passed on to the build process as your preprocessor macro(s).
See the answer to this, with the additional step of setting up ResourceCompile options, i.e. edit your project file in a text editor to include elements like this:
<ItemDefinitionGroup>
<ClCompile>
<AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<ResourceCompile>
<AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)</AdditionalOptions>
</ResourceCompile>
</ItemDefinitionGroup>
The difficulty I have been running into is that using Visual Studio 2010, in order to pass a preprocessor definition to the resource compiler you must define it in the project settings (Config Properties -> Resources -> General).
You got the hard part. Just enter the define as TESTAPPLE=15 and it will effectively #define TESTAPPLE 15 for the entire project.
Although this is an old question, but for any reference now, the following steps work with me:
Pass the variable = underlying value in msbuild command line using /p, e.g.
msbuild my_solution.sln /p:ENVSOMEVALUE="4"
In the project file my_proj.vcxproj, define the variable as a preprocessor definition for the resource compiler, and assign it to the variable passed from msbuild, e.g. add the following section inside <Project></Project>:
<ItemDefinitionGroup>
<ResourceCompile>
<PreprocessorDefinitions>
SOMEVALUE=$(ENVSOMEVALUE);
%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
</ItemDefinitionGroup>
in the .rc file, use the code the way you like:
#if SOMEVALUE == 4
You might need to set a default value for the definition if not passed from msbuild (e.g. building from Visual Studio IDE), in this case you might use the condition manually in the project file, e.g.
<ItemDefinitionGroup Condition="'$(ENVSOMEVALUE)'!=''">
<ResourceCompile>
<PreprocessorDefinitions>
SOMEVALUE=$(ENVSOMEVALUE);
%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(ENVSOMEVALUE)'==''">
<ResourceCompile>
<PreprocessorDefinitions>
SOMEVALUE=1; <!-- Default Value -->
%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
</ItemDefinitionGroup>