I just started learning C++ today. With previous knowledge in other languages I am doing fine so far, but I am confused about #define directives.
I have this code in my "review" C++ file:
#include <iostream>
#define TEST //object-like macro
#ifdef TEST //if TEST is defined
std::cout << "This works!" << std::endl;
#endif
#ifndef NOT_TEST //if NOT_TEST is NOT defined
std::wcout << "This also works!" << std::endl;
#endif
int main()
{
//program code
}
The above code produces errors, first one being syntax error : mssing ';' before '<<'. When I move the #define/#if directives into the main loop, it works properly:
#include <iostream>
int main()
{
#define TEST //object-like macro
#ifdef TEST //if TEST is defined
std::cout << "This works!" << std::endl;
#endif
#ifndef NOT_TEST //if NOT_TEST is NOT defined
std::wcout << "This also works!" << std::endl;
#endif
}
What about the first block of code is incorrect? based on the tutorial I am using, I thought that was how it was supposed to be formatted?
EDIT: I've updated my code to be more clear.
This has nothing to do with formatting. In fact, C++ files are formatting-agnostic. The problem is that after the pre-processor parses your file, you end up with 2 cout statements outside the main function. In C++ you cannot have standalone statements outside of a function, except for declarations/definitions.
You should understand that compilation of a C++ program is a two-step process. First the preproccessor is executed that transforms the file according to instructions you gave it (those that start with #). Then the C++ compiler is executed on the resulting file.
The “code relating to the preprocessor” here is just the #ifdef/#ifndef (with the condition that follows it immediately) and #endif keywords and, indeed, you can put those wherever you like, since preprocessor doesn’t care about C++ syntax, it performs straightforward string operations.
When the preprocessor runs it plugs your C++ code between #ifdef and #endif into the file, so the effect is the same as if you just had the cout << … line (outside the main function).
So, since TEST is defined and NOT_TEST is not, after the preprocessor did its job you are left with a file that has just two lines:
std::cout << "This works!" << std::endl;
std::wcout << "This also works!" << std::endl;
If you try to compile it, you’ll see that the compiler is not happy, because that’s, obviously, not a valid C++ program.
The Problem is using of cout << out of main function.
If you wanna have any output line you can use #error directive to abort compilation process.
Related
Let's assume that I have a program with many files and many cout's, and for debug purposes I would like to disable output for a moment, but only in a single file.
It is just out of curiosity question, I know that I shouldn't do that to disable output, but I started to wonder when facing this problem, and it only shows properly what I mean.
I tried to create a #define macro, but I can't replace the whole line, only a single symbol (with params).
For example:
//some common header file
#ifdef DISABLE_OUTPUT
#define cout... void; //?? DK exactly what should i put here
#endif
//other file
#include "commons.h" //my macro
#define DISABLE_OUTPUT
void foo()
{
...
cout << "blablabla" << 4 << "something" << endl; // should be removed
...
}
If DISABLE_OUTPUT is defined, the whole line should be replaced with void; (better clear the line).
The problem is that I don't know how to clear the whole line with #define.
Is there any "magic" symbol or trick that I can use?
It’s a bad idea to define a macro with the same name as a standard library component, so you shouldn’t #define cout at all. I’m going to assume you #define disableable_cout instead.
The simplest answer would be to define it like this:
#ifdef DISABLE_OUTPUT
#define disableable_cout if (false) cout
#else
#define disableable_cout cout
#endif
And then update the cout line in foo to this:
disableable_cout << "blablabla" << 4 << "something" << endl;
Which would expand to either this:
if (false) cout << "blablabla" << 4 << "something" << endl;
if DISABLE_OUTPUT is defined, or to this:
cout << "blablabla" << 4 << "something" << endl;
if DISABLE_OUTPUT were not defined.
Then, if DISABLE_OUTPUT is defined, the output line is skipped; if not, it will happen.
Alternately, you could require DISABLE_OUTPUT is always defined, to either 0 (don’t disable) or 1 (do disable). Then you could use a single definition, like this:
#define disableable_cout if (!DISABLE_OUTPUT) cout
Note that, either option is fragile, like most macros, but it should work in the typical case.
If we want to use user input to do something in a program, or print a result we need to
#include <iostream>
otherwise, cout and cin will not be acknowledged by the compiler.However the command #include is a pre-processor command. And when I was writing my program the following happened. I wrote the following code :
#define PRINT_DEBUG_INFO(a) {cout << “Info: ” << a << endl;}
#include <iostream>
And no errors popped up.How is it possible to use cout before including iostream? Even if I declare the PRINT_DEBUG_INFO(a) without including iostream, I don't get a compiling error.
Can somebody explain me why this happens?
The preprocessor doesn't require any C++ declared symbols to be evaluated to do its work.
It's pure text processing, so defining a macro like
#define PRINT_DEBUG_INFO(a) {cout << “Info: ” << a << endl;}
and expanding it like
#include <iostream>
void foo {
int a = 5;
PRINT_DEBUG_INFO(a);
}
will become
// All the literal stuff appearing in <iostream>
void foo {
int a = 5;
{cout << “Info: ” << a << endl;};
}
So there's nothing checked regarding proper C++ syntax during definition or expansion of the macro.
These statements will be processed further by the C++ compiler, which will complain about cout isn't declared in the global scope.
To fix this, declare your macro like
#define PRINT_DEBUG_INFO(a) {std::cout << “Info: ” << a << std::endl;}
You define PRINT_DEBUG_INFO but you don't use it so there is nothing for the compiler to compile or complain about.
You are just defining PRINT_DEBUG_INFO(a) and not using it. When you actually use it inside your program you will get the error that cout is not defined.
When you are not actually using it, the compiler finds no place to substitute the defined constant. When you actually use it, the program gets expanded during compilation and shows you the error.
And moreover there is a bracket in your macro which gets expanded with brackets and may lead to error.
I have a simple C++ program;
int someFunction()
{
cout << "Testing here" << endl;
cout << "reached here in function " << __LINE__ << " in " << __FUNCTION__ << endl; // debug purposes
// do some more stuffs here
cout << "reached here in function " << __LINE__ << " in " << __FUNCTION__ << endl; // debug purpsoes
}
Is there a way of switching on/off the lines of codes marked as "debug purposes"? The ideas is in case of problems I can just write a one liner to switch on those kind of debug purpose codes and when solved switch them off.
I know debuggers are for this very purpose, but want something simple for simple programs.
You can wrap it in a conditional define:
#ifndef NDEBUG
// Debugcode here
#endif
When you are done debugging, just define NDEBUG during compilation. You might also want to have a look at assert. It is controlled by the same macro NDEBUG and easily lets you check conditions in you program. You can also have it produce meaningful error messages:
assert(allWentWell && "Blah went wrong!");
The error-message of assert will also include information to where the error occurred.
The classical way to solve this is to define some logging macros; a simple example may be:
#ifdef_NDEBUG
# define LOG(X)
#else
# define LOG(X) do { std::clog<<__FILE__<<":"<<__LINE__<<" "<<(X)<<std::endl;} while(0)
#endif
Usage:
LOG("Before frobbing the widget (i="<<i<<")");
Of course this can be taken to any level of complexity (there are quite a few libraries that approach the problem of logging).
In addition of the other replies, you could declare a global flag:
#ifndef NDEBUG
bool want_debug;
#endif /*NDEBUG*/
then define a macro
#ifndef NDEBUG
#define DEBUG_OUT(Expr) do {if (want_debug) \
cout << __FILE__ << ":" << __LINE__ << " " \
<< Expr << endl;} while(0)
#else
#define DEBUG_Out(Expr) do{}while(0)
#endif
and you'll add a lot of statements like
DEBUG_OUT("here x="<< x);
The want_debug flag could be set at runtime (e.g. inside a debugger, or with some -d program argument handled by your main). If you compile with -DNDEBUG you won't get any code for DEBUG_OUT statements.
I'm using the NDEBUG preprocessor symbol related to the old assert(3).
Of course, be careful to avoid meaningful side-effects in DEBUG_OUT, e.g. DEBUG_OUT("here y=" << y++); /*WRONG side effect*/ is certainly a mistake.
If using a recent GCC compiler (e.g. g++ version 4.9), you could also customize it using MELT by adding magically (in your customizing extension coded in MELT) some pass which would automatically add logging messages (e.g. at end of every routine). But that might means weeks of work (so is worthwhile for big existing software projects).
Is you want to "switch on/off" some part of code, you can use macros and preprocessor directives #ifdef and #endif:
#ifdef _DEBUG
//run this code
#endif
Above code will only run, if _DEBUG macro is defined:
#define _DEBUG
I have the following bit of code, I expect that given cstdio is included that the first line will be printed, however the second line is printed.
What am I doing wrong? is it possible to know if labels such as printf or strncmp or memcpy have been defined in the current translation unit at compile time?
#include <iostream>
#include <cstdio>
int main()
{
#ifdef printf
std::cout << "printf is defined.\n";
#else
std::cout << "printf NOT defined!\n";
#endif
return 0;
}
Is the reason because the preprocessor is run before variables and labels are introduced into the scope/TU?
In short is the following code bogus? :
http://code.google.com/p/cmockery/source/browse/trunk/src/example/calculator.c#35
#ifdef only applies to preprocessor macros, defined with #define, not to symbols like function names and variables. You can imagine the preprocessor as an actual separate preliminary step, like running your code through a perl script, that occurs before the "real" compiler gets a crack at it.
So there is no programmatic way to check whether symbols like printf are defined in the current scope. If you use one and it's not defined, you'll get a compiler error. The normal thing to do is to #include a header file with the required definition in the source file where you reference it, not to write a source file that will adapt itself to different possible sets of headers.
As a hack, and depending on your environment and specific problem, the header file that does define printf (or whatever function you care about) may also contain some preprocessor #defines that you could check for.
You can use the guards from original include files to determine if they were included and, consequently, functions were declared.
For example, <stdio.h> shipped with my MSVS2010 has _INC_STDIO guards. Thus your code should be like:
int main()
{
#ifdef _INC_STDIO
std::cout << "printf is defined.\n";
#else
std::cout << "printf NOT defined!\n";
#endif
return 0;
}
Note that this solution is environment-dependent, so you should create more complicated rules it you are supposed to support more than one build chain.
Their are a large number of symbols in stdio.h which are #defined, and it is imported by cstdio
So you could use
#include <iostream>
#include <cstdio>
int main()
{
#ifdef stdin
std::cout << "printf is defined.\n";
#else
std::cout << "printf NOT defined!\n";
#endif
return 0;
}
WARNING I've looked at the header but not tested.
#define cimg_use_jpeg 2
#ifndef cimg_use_jpeg
//code goes here
#endif
I really don't understand...
Every time things similar to this (so-called impossible things) happen me, the reason is: The code I see in my editor is not the code that runs. This can happen in many ways.
forgetting to make
forgetting to save
saving a copy of the code
running in a different evironment from the one you compiled to
the new code ran but I opened and am looking at the old output
It cannot be executed. Maybe you run incorrect executable version. Printing __DATE__ and __TIME__ to trace or log helps to detect such errors.
This should definately work as you expect. The code in the #ifndef/#endif block will not even be compiled by the compiler, assuming the cimg_use_jpeg is defined as expected.
So the question is, is the cimg_use_jpeg defined as you expect? Or are you simplifying your question and by virtue of the simplification providing a sample that actually works?
#Anders K. This works, i.e. the proper things are not executed. The code produces "BAZ is undefined"
#define FOO 2
#define BAR
int main(int argc, char* argv[])
{
#ifndef FOO
std::cout << "FOO is undefined" << std::endl;
#endif
#ifndef BAR
std::cout << "BAR is undefined" << std::endl;
#endif
#ifndef BAZ
std::cout << "BAZ is undefined" << std::endl;
#endif
}
Either you write
#define yoursymbol
#ifdef yoursymbol
or you write
#define yoursymbol somevalue
#if yousymbol == somevalue
don't mix them
EDIT:
since people seem to misunderstand what I meant here's a clarification: when you program it is important to code so that other people understand what your intentions are, so if your intention is only to check whether a symbol is defined it is confusing to specify a numerical value.