C Standard Detection in g
In C , the standard compiler version may affect the behavior and compatibility of your code. When compiling with g , understanding the default standard is crucial.
Consider the following code snippet:
#include <fstream> #include <string> int main() { std::string filename = "input.txt"; std::ifstream in(filename); return 0; }
If you compile this code on Windows using the g example.cpp command, it may fail due to linker errors related to converting from std::string to const char*. However, specifying a specific C standard using g -std=c 17 example.cpp resolves this issue.
To determine the default C standard used by g , you can execute the following command:
g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
For example, using this command on the Ubuntu 4.8.4 version of g , you would get the output:
#define __cplusplus 199711L
This indicates that the default C standard for this version of g is C 98 (199711L represents the November 1997 revision of the C standard).
It is recommended to always specify the desired C standard explicitly when compiling with g . This ensures that the code conforms to the intended standard and avoids potential compatibility issues that may arise from using an outdated or undesired standard.
The above is the detailed content of How to Determine the Default C Standard Used by g ?. For more information, please follow other related articles on the PHP Chinese website!