Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
Encountered an inexplicable situation trying to build a Linux project with Visual Studio Professional 2019: some preprocessor switches seemed to be ignored by g++ preprocessor -in header files only.
Found some related questions, but users seemed to have found their own issues by either enabling -v (verbose) option, or explicitly passing the preprocessor options with -D flag.
Although I've tried both, neither worked for me.
Some of the warnings were about "unknown pragma" messages, which looked inoffensive, making me believe the preprocessor recovered from these warnings -otherwise they should have been errors, not warnings, right ?
These are the pragmas:
#pragma warning (disable:4996)
#pragma warning (disable:4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
#pragma warning (disable:4503) // decorated name length exceeded, name was truncated
It was only until I put those pragmas inside WIN32 compilation switch that I managed to compile that project.
The pragmas were not actually targeting the Linux distribution, but the Windows one -for the Microsoft C++ preprocessor. Thus, on the compilation switch, the g++ preprocessor would not stumble on these "unknown pragmas" and then, the preprocessor switches in the header files were taken into account and the project compiled. The project is a huge one, and generated tens of thousands of compilation errors, cannot be put here, but I hope I provided enough explanations around the issue.
Did anybody encountered this too, and can point me to the documentation lines that g++ preprocessor would not "recover" after few warnings related to "unknown pragmas" ?
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am compiling a large project for several platforms using GCC and Clang. The issue I have is that I do all of the bug fixing and testing on one platform (Ubuntu 18.04), and even run static tools like cppcheck and clang-tidy to find bugs. As part of the bug fixing, I even try to compile with several compilers on Ubuntu to make sure that the code is ready to ship.
However, several times I have run across the problem where a developer on another system can't compile the update due to a simple missing include.
A recent example is where we introduced some new functionality which was heavily tested in GCC and Clang on Ubuntu. Then a dev on MacOS got some compiler errors which turned out to be due to a missing #include <array> in one file, and missing #include <sstream> in another. I mean, when you look at the offending files, they were indeed using arrays and stringstreams, so I get it. But I am just surprised that the static tools didn't catch those errors.
So how do I solve this problem? They definitely are programming errors, not compiler bugs since it was obvious that I should have included the files.
You are looking for include-what-you-use. From their docs:
"Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo.h should #include a .h file that exports the declaration of that symbol.
Compiling it yourself isn't trivial as the inner workings of this tool is tightly coupled to the Llvm internals. But you might be lucky to find a pre-built package in your distro. Still, once you get it running, it's not a silver bullet. The problem it tries to solve is hard, there might be false positives etc.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I have the Windows 10 OS, use VS Code to write my C++ code and use CMD to compile my programs. I don't really know which standard the compiler in my PC (MinGW, gcc version 6.3.0) uses, but I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17 every time I need to compile my program using that standard. How do I set the desired standard as default?
Unfortunately, I need to type in -std=c++17 every time I need to compile
This is why build scripts exist. There are many arguments that you want to pass to your compiler at some point:
Source files (you may have multiple translation units = .cpp files)
Include directories
Libraries to link
Optimization level
Warnings
C++ standard
Symbol defines
...and many more compiler flags...
In bigger projects you may also have multiple build targets (and thus compiler invocations) and you don't want to do all that by hand every time either.
As a simple solution, you could write a .bat script that invokes the compiler with the right arguments for you. However, there are tools that do a way better job at this, such as make (generally only found in the Linux world) or MSBuild (part of Visual Studio). Then there are also tools that generate these scripts for you, such as CMake and several IDEs with their own project configuration files.
I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17 every time
-std=c++17 is exactly how you ensure that you're using the C++ version you want (in this case C++17).
I'm using Visual Studio Code for c++ with MinGW and g++. My code uses a couple of g++ predefined macros. These work in the compiler without any problem, but Intellisense doesn't know about the macros and that causes some misdiagnosed errors (and other Intellisense problems) in the editor.
I can get all the predefined macros for my particular configuration of g++ with this command:
g++ -dM -E foo.cpp > defines.h
And this allows me insert them into the "defines" property of the c_cpp_properties.json file. This solution is a hack though, and if I'm not careful it could completely undermine the purpose of these macros. I want the compiler and Intellisense to cooperate across multiple development platforms, and that's looking like a pretty complicated setup.
Is there a better way to let Intellisense know about the g++ predefined macros?
From what I can tell, Intellisense's ability to properly handle preprocessor macros is still limited. If you are using CMake for Makefiles, it appears that you can tell Intellisense what macros you have, as seen here: auto-defined macros. I have not played with this personally, but it would be worth investigating.
If you can't get this to work, I found a feature to just turn off the macro-based highlighting. In settings, set"C_Cpp.dimInactiveRegions" to false. This isn't ideal, because it stops all graying out, including debug blocks like if(0) {...}, but it could be a temporary fix and you might find it less irritating.
Apart from that, look closely for added features in future updates. I'll try to update this post when I find any new discoveries on the matter.
The property in c_cpp_properties.json called compilerPath allows IntelliSense to query the compiler for default include paths and defines. It may be necessary to set intelliSenseMode to gcc-x64 for this to work, as that is not the default on Windows. (I currently do not have a Windows setup so I can't test this for the time being)
I am working with c++ code base which emits many warnings due to which its hard to catch or notice new warnings being introduce by code I am adding or changing.
this is painful as I am not going to spend time resolving all the warning coming due to other modules, but I certainly do not want to add code which emits warning.
I wonder if there is some tool in gcc or external which can help here.
I can think of a painful way of taking a diff of compiler output with and without my code , but it will make my coffee taste much bitter.
Any suggestion on this?
If the problem stems from third-party source files, you can build some files with warning flags on, and other files with warning flags off. GCC has a whole range of well-documented warning control options.
If the problem stems from third-party headers that you include in your code, you can use -isystem to have headers under that path treated as "system headers", whose warnings are typically ignored.
If the code is more entwined, you are out of luck.
This question already has answers here:
Is there a standalone C++ source preprocessor? [closed]
(7 answers)
Closed 8 years ago.
I was going through this book - An Introduction to GCC by Brian Gough where he talks about the GNU C preprocessor cpp and how its provided as a separate package even though its integrated into the compiler.
I was wondering if there is a similar command that is provided as a part of the g++ package that can show me all the expanded macros before compiling the source code.
I know there is the -E option supported by both gcc and g++ which stops after the preprocessing stage but was curious if there is something similar to cpp in g++
At the preprocessing level, there isn't much difference between the two languages. It's just a matter of telling the preprocessor how to resolve includes (i.e. telling it the correct include paths) and defining certain macros (like __cplusplus). So the same program you use to expand preprocessor code in C should do the job for C++.