Embed a string of compiler options in an executable (Visual Studio) - c++

Using gcc and Makefiles, I am able to embed some compiler options into a string for an executable to access as follows:
Makefile:
CXX=/usr/bin/g++
CXXFLAGS=-ansi -Wall -c
CXXDEFINES='-DCXXCOMMAND="$(CXX) $(CXXFLAGS)"'
C++ file:
#ifdef CXXCOMMAND
std::cout << "C++ Command: " << CXXCOMMAND << std::endl;
#endif
And then I am able to print it to the console from the executable.
Is it possible to do this in Visual Studio? Specifically I am looking to put in the optimization flags (/O2, /Od, etc...), but the more I have access to, the better things will work for me.
Microsoft Visual Studio Professional 2012
Intel C++ Composer XE 2013 SP1

Yes, sort of. You can define a macro in the project properties that contains a string, and embed within it any project property values you want. However, these properties, while used to construct the command line, are not the actual command line flags themselves.
For example, add the following as a Preprocessor Definition in the C/C++ Preprocessor project properties:
CXXCOMMAND="%(Optimization)"
Then CXXCOMMAND is "MinSpace" for a build set to compile with /O1.
Just be careful to have only an even number of quotes in your preprocessor define...

Related

VSCode C++ Intellisense can't discern C++20 features

I try to run codes like
#include <string>
#include <iostream>
int main() {
std::string str = "This is a string";
std::cout << str.starts_with("name");
}
But intellisense will give out an error
"std::__cxx11::basic_string<char, std::char_traits,
std::allocator>" has no member "starts_with" C/C++(135) [6,9]
And It still can be build and produce a correct result.
Also it can find implementation in header file.
But the macro __cplusplus is defined as 201703L
I've already added a command -std=c++20 when building, why this happened?
Compiler: minGW 11.2 compiled by msys2
Assuming you are using Microsoft's C/C++ extension, you must configure the extension to use C++ 20 standard for intellisense.
The easiest way to do this is to add the line "C_Cpp.default.cppStandard": "c++20" to your settings.json file. You can also find the setting in the GUI under the name "Cpp Standard". Selecting c++20 from its dropdown will achieve the same result.
Note that this setting is, by default, set as a global user defaults. You can configure it per-workspace by selecting the Workspace tab in the settings GUI and changing that Cpp Standard dropdown to c++20.
As for why adding the -std=c++20 flag didn't work: -std=c++20 just tells your compiler which standard to use to build your code. 'Intellisense' does not receive this flag because it is a separate tool from the compiler and is therefore not required to support all the standards the compiler supports. It may support less even, although Intellisense tools usually support as current a standard as possible. Therefore the language standard for Intellisense must be configured separately from the compiler (in this case).
Final Note: After changing the setting, try closing and re-opening VS Code. In my experience changing the language standard setting can cause some weirdness to happen. Closing and re-opening VS Code seems to ensure the setting changes take full effect.

Eclipse CDT for Linux

I have written a program using C++11 features.
/* * test.cpp * * Created on: 05-Jul-2015 * Author: avirup */
#include<vector>
#include<iterator>
#include<iostream>
using namespace std;
int main() {
vector<int> v;
v.push_back(5);
v.push_back(7);
for(auto i=v.begin();i!=v.end();i++) {
cout<<*i<<endl;
}
for(auto i=v.cbegin();i!=v.cend();++i) {
cout<<*i<<endl;
}
}
The program is compiling correctly and showing results but the editor is showing but red lines below valid functions like cbegin() and cend() which are constant reference iterators. which is annoying. How to get rid of this?
Just for completeness since this has no answer and give an explanation.
To achieve compiling with C++ 11 (or another version) and Eclipse actually supporting it you need to do two things.
First the compiler flag needs to set so -std=c++11 or -std=c++0x is appended when calling g++ or whatever is used.
Open the properties for the properties for the project. Select it and right click ↦ Properties (or Alt+Enter for Windows users)
Go to C/C++ Build ↦ Settings, maybe select your preferred configuration, ↦ GCC C++ Compiler (or any other compiler you use) ↦ Dialect.
Select from the combo or write the flag to the Other dialect flags if not present in the combo (like -std=gnu++14 or -std=c++1z).
CDT will now call your compiler with -std=c++0x when compiling. Now to the part so CDT supports C++11 and does not show errors for missing types and such. My libstdc++ contains lines like
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else // C++0x
that causes your errors and the actual type declarations/definitions are greyed out if you view them in the C/C++ editor. __cplusplus needs to be set correctly with #define __cplusplus 201103L so they will be parsed and indexed by CDT. This is also done via the project settings or can also be done for the whole workspace.
Go again to the project settings.
C/C++ General ↦ Preprocessor Include Paths, Macros etc., also maybe select your preferred configuration, ↦ tab Providers.
Select the entry for your compiler (for me it’s CDT GCC Built-in Compiler Settings MinGW.
Add -std=c++11 or -std=c++0x to the Command to get compiler specs textfield at any location.
Optional: Select Allocate console in the Console View and hit apply. You should now see something like #define __cplusplus 201103L in the console.
To set it for the whole workspace just check Use global provider shared between projects and click on Workspace Settings where an almost identical dialog opens.
I’m currently writing a plug-in that extends the new C/C++ project wizard where one can select the C++ version for the project that sets the compiler flag correctly and also the above setting for the indexer and some other stuff. But dunno if it will ever will be integrated into CDT or needs to be installed via plug-in. But it sure will be in https://www.cevelop.com in a few months.

Setting up MingW and Code::Blocks in Windows 8 64 bit

I am trying to setup MingW and Code::Blocks on my Windows 8 64 bit laptop, and I'm facing some problem while building a main.cpp file. These are the versions that I have installed:
x86_64-w64-mingw32-gcc-4.7.4-release-win64_rubenvb.7z for MingW (4th one in that list), and
codeblocks-12.11-setup.exe for Code::Blocks.
I've set the path to mingw64\bin in the environment variable. Also, in the Code::Blocks compiler settings, I have set path for all ToolChain Executables. There are two gcc in the MingW bin path. One is - gcc.exe and other is - x86_64-w64-mingw32-gcc.exe. Same for C++ Compiler, Linker for static and dynamic libs. Now when I try to build a simple Hello World file, it shows some errors:
obj\Debug\main.o -- In function swscanf
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
obj\Debug\main.o -- In function `wscanf':
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
Similarly there are many errors and undefined references for - fwprintf, wprintf, std:cout, etc..
I'm sure there is some configuration problem, but I cannot find out what's the issue. Can any one take a look at the problem.
Here's the code I'm running:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
If you don't require 64 bit support, consider downloading "codeblocks-12.11mingw-setup.exe"
It packs its own 32 bit mingw and has everything preconfigured for you.
Otherwise, if you still need to compile amd64 apps:-
Assuming you extracted to D:\Mingw64, under the "Tool Chain Executables" tab, set the "Compiler's Installation Directory" to "D:\Mingw64\bin", Then click auto-detect to check whether code::blocks likes what it finds.
If everythings Ok, make sure the rest of the fields are as follows:-
C compiler: x86_64-w64-mingw32-gcc.exe
C++ compiler: x86_64-w64-mingw32-g++.exe
Linker for dynamic libs: x86_64-w64-mingw32-g++.exe
Linker for static libs: x86_64-w64-mingw32-ar.exe
Debugger: GDB/CDB Debugger...
Ressource compiler: x86_64-w64-mingw32-windres.exe
Make program: mingw32-make.exe
Click on the "Additional Paths" tab and enter the following line:- (the path is correct for ver 4.7.4,the one you downloaded. It ends with 4.7.3)
D:\Mingw64\libexec\gcc\x86_64-w64-mingw32\4.7.3\
Click on the "Search directories" tab to the imediate left "Toolchain Executables"
Add the following paths:-
D:\Mingw64\include
D:\Mingw64\x86_64-w64-mingw32\include
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\backward
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\x86_64-w64-mingw32
D:\Mingw64\lib\gcc\x86_64-w64-mingw32\4.7.3\include
Click on the "linker" tab and add the following paths:-
D:\Mingw64\Lib
D:\Mingw64\x86_64-w64-mingw32\lib
And finally click on the "Compiler Settings tab", Click on "Other options" and add:-
-m64
One last thing-> remember to save those settings to disk!
If it crashes while you test this new config, you might wish you had!
Do this (I think) either by "File->Save Everything" or restarting code::blocks ...without another instance running.
Try compiling something then share the outcome
Peace! Dear brother, I think you need this:
(1)
std::cout << "Hello world!" << std::endl;
You need to put "std::function_name" whenever you are using a predefined function from iostream.h, because this is you way access it in GCC, which Code::Blocks uses.
(2) Try it compiling by omitting "using namespace std;" from your code.
Because I've compiled C and C++ code many times in Code::Blocks on 32-bit Windows XP, and it did!
(3) Make sure the path is set to the directory "mingw\bin".
(4) Ensure if you have 64-bit MinGW installed and path set to its "bin" directory (if you want to compile for 64-bit). It will compile for 64-bit by default.
(5) Try the flags/arguments "-m32" and "-m64" for compiling for 32- and 64-big respectively.

NetBeans C++ - way to check compiled with debug in code?

I'd like to be able to put some macro commands in my C++ code running NetBeans (and GCC) eg.
#ifdef DEBUG
std::cout << "DistributionSuper constructor called" << std::endl;
#endif
Does anyone know if there's a way to use a debug compilation flag in NetBeans without having to do a -DDEBUG defines on the gcc command line? Checking the g++ compile lines didn't point to any defines I could use specific to debug, but I was hoping NetBeans might have a compile variable that does this somewhere. NetBeans does have some script variable like ${CND_CONF} that might help, but I can't really see a way to get at these in the C++ code.
Thanks guys
Pete
After hunting around, couldn't find anything that would let you check debug status in the code with NetBeans compiles. Easiest way to solve the problem seems to be to do an explicity -DDEBUG in the project options, then use:
#ifdef DEBUG
#warning In debug mode
std::cout << "I was compiled with a DEBUG define in the g++ command line
#endif

How to compile Mathematica to C code with intel C++ compiler (x86) on Win7(x64)?

My OS is windows 7 64bit. I installed Microsoft C++ 2008 Express edition and Intel C++ Compiler v11.1 x86 version.
Now I could successfully compile x86 C code in Mathematica, e.g.
In[1]:= Needs["CCompilerDriver`"]
In[2]:= greeter = CreateExecutable[StringJoin["#include <stdio.h>\n", "int main(){\n", " printf(\"Hello world.\\n\");\n", "}\n"], "hiworld",
"Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler,
"CompilerInstallation" -> "C:\\Program Files (x86)\\Intel\\Compiler\\11.1\\072\\",
"CompilerName" -> Automatic, "TargetSystemID" -> "Windows"]
Out[2]= "C:\\...\\AppData\\Roaming\\Mathematica\\\
SystemFiles\\LibraryResources\\Windows-x86-64\\hiworld.exe"
But failed to use CompilationTarget -> "C" to compile a function like this
In[3]:= f = Compile[{x, y}, Sqrt[x^2 + y^2], CompilationTarget -> "C"]
During evaluation of In[3]:= LibraryFunction::libload: The function compiledFunction5 was not loaded from the file C:\\...\AppData\Roaming\Mathematica\ApplicationData\CCompilerDriver\BuildFolder\vax-5844\compiledFunction5.dll. >>
During evaluation of In[3]:= Compile::nogen: A library could not be generated from the compiled function. >>
I guess I need to specify a default "TargetSystemID"-> "Windows" since my platform is x64, but don't know how to setting such an option in Mathematica.
Did I miss any point here?
PS: I don't want to install Microsoft Visual Studio recently.
In your first code block you specified the compiler that you wanted. In the second you didn't - so it might be that Mathematica does not "know" about the compilers that you have installed.
Run Needs["CCompilerDriver`"] then CCompilers[Full] to see which of your compilers Mathematica knows about. Also maybe look at the global $CCompilerDirectory.
If your Intel and/or Microsoft compilers are not showing up, then follow the Specific Compilers page of the CCompilerDriver User Guide.
To get your Intel compiler working, I think maybe the following should suffice:
$CCompiler = {
"Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler,
"CompilerInstallation"->"C:\\Program Files (x86)\\Intel\\Compiler\\11.1\\072\\"}
That said - if I use the above code to change the directory to my compiler to something wrong, then the first error type I get (before the Compile::nogen) is CreateLibrary::instl, not the LibraryFunction::libload message that you have. Maybe something is wrong with your default build directory: $CCompilerDefaultDirectory...
In response to your PS, the Intel C++ v11.1 compiler for Windows is on the list of compilers that they have tested, so you shouldn't need to install MS Visual Studio. On the other hand, you could try GCC for Windows via MiniGW or CygWin (also see SO/187990).