Why and when do we need to use #pragma [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I was learning a winsock client server program and came up through #pragma comment(lib,"ws2_32.lib"). Understood its need here. But what are the other instances I can use it and why do I need to use it?

#pragma 's are implementation defined compiler commands.
That means, each compiler could treat or support pragmas in a different way.
They are used for example for generating user defined warnings or errors as with
#pragma warning WARNINGMSG
or #pragma error ERRORMSG
or as include guard with #pragma once at the top of a headerfile.
What #pragma comment is used for, you can find nice explained here:
What does "#pragma comment" mean?
But after all, I would advise you to avoid pragmas as far it is possible, because as they are almost all, implementation defined, your code will be limited in portability if you use them.

Related

what could cause pragma once in c / cpp file [closed]

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 1 year ago.
Improve this question
I'm wondering what could go wrong when I will have bigger C / C++ project with multiple modules and I will by mistake type #pragma once into some c / cpp file. Will it just spit warnings at me and ignore the pragma or can it somehow break my project?
EDIT:
This is just teoretical question. There are no more details.
#pragma once serves as a header guard similar to:
#ifndef MY_INCLUDED_FILE
#define MY_INCLUDED_FILE
....
#endif
This just makes sure that a certain file is compiled only once even if it is included multiple times.
If a compiler doesn't support a certain pragma it will simply ignore it.
On older compilers it may not be known, so you have to use the ifdef solution. If the compiler doesn't understand it, and you don't have the header guard, then you probably will get a lot of errors for duplicate definitions and similar.

Why comment cannot execute and ignore by the compileres ,in which header file it's defination exit? [closed]

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 2 years ago.
Improve this question
I am unable to understand the behavior of comments in C++. Why comment not executed by the compiler , what's the main reason, and in which header file the definition of comment can exit.
Thanks
Comments (single line or multi lined) are not a part of the executable/ library. It is just in place, basically to document the code and make it easier to understand what's going on. Sometimes when the same source or header file is edited by multiple programmers, the other programmers might not know what a particular statement would do, how much it costs (performance and/ or memory), if it might throw an error and so on. The compiler basically ignores these statements as they are not needed to build the actual compiler output.
C++ has two main comment syntaxes,
//: Single line comments.
/* */: Multi line comments.

Comment blocks of code in c++ [closed]

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 years ago.
Improve this question
I'm having difficulty formulating this question, and therefore, I can't search for the answer in google.
My question is whether a function exists which can auto-comment/auto-uncomment blocks of code. Or is there a way to automatically remove or ignore certain lines when the code is built?
Maybe it can be done with directives?
It sounds like you are asking about #if which skips sections of code based on compiler settings.
Microsoft's documentation on the feature is located on MSDN note that this link may contain compiler specific rules (I didn't read through all of it).
I'm having difficulty understanding your question. Are you looking for a shortcut to automatically comment out a section of code? If so, consider this, from the Codeblocks manual:
Comment highlighted code | Ctrl-Shift-C
Uncomment highlighted code | Ctrl-Shift-X

fopen64/ fseeko64 and c++ 11 [closed]

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 7 years ago.
Improve this question
Im writing a code in c++ with codeblocks. I included the flag : " get g++ follow c++ 11.." In the compiler settings to use chrono class, but after i included this flag i couldent compile the program because suddenly the function fopen64 "was not declared in the scope". Just so you know- i can compile the program without the flag.
How can i use both fopen64 and chrono class? Is this possible? And if not, is there other precise time measurement class so i can use to mesure microseconds?
Thanks!
The fopen64() function is from the Linux Large File Support library. It’s just a version of fopen() that supports files larger than 2Gbi. On a modern system, you can use the standard library function, fopen(). On Posix, you can use fseeko() and ftello() and on Windows, _fseeki64() and _ftelli64(). That said, defining _LARGEFILE_SOURCE might work on your compiler.

When to use include guards or #pragma once C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration?
I am tempted to put it in every header, but I'm afraid it would be unneeded and only add to the compile time. What is good practice or common to do?
Let me clarify: I understand the difference between the two. I am asking whether by experience programmers use it in every file or just those that require it.
Summarizing the comment by Galik and what I realized:
Include guards should be put in every header file in the case that something in the future conflicts. Furthermore, the small time it takes the compiler to process the include guards will make the compilation faster since the extra header does not need to be processed.
#pragma once is compiler specific, while normal include guard definitions, should work with any c++ preprocessor.
Thus for sake of portability, always stick to "ye good old" include guards idiom.
The use of include guards is essential to prevent multiple symbol declaration errors in your code. You shouldn't bother about assumptions, or implications, how this is handled by the c++ preprocessor (modern ones are pretty optimized to do this efficiently).
If you want your code to be portable to all C++ compilers you'll need to use include guards. If you feel the compiler you use is inferior and benefits from the use of #pragma once you can also add this pragma: compilers not understanding it will just ignore it. Personally, I don't bother with use of #pragma once. It is a solution to a non-existing problem: compilers can absolutely detect include guards to avoid opening files already included.