![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 10
| #ifdef _cplusplus I have the following code - FIRST.CPP, CALL.C and stdafx.h FIRST.CPP: Code: #include "stdafx.h"
extern "C" void callmethod();
int _tmain(int argc, _TCHAR* argv[])
{
callmethod();
return 0;
}
void justcall(){
#ifdef __cplusplus
printf("This is C++");
#else
printf("This is C code");
#endif;
}
Code: void callmethod(){
justcall();
}
Code: #include <stdio.h> #include <tchar.h> extern "C" void justcall(); The output when I run FIRST.CPP is "This is C++". From FIRST.CPP, I call callmethod() which is defined in a .c file and from this callmethod() I call justcall() which is in a .cpp file. My question is justcall() is called from a C program then why is the code inside #ifdef _cplusplus getting executed? Thanks, Chandana. |
| Chandana is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,459
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Aug 2009
Posts: 10
| Thanks for the reply. what should I do if I want to get "This is C code" as the output. |
| Chandana is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,459
| What are you actually trying to do? After all, taken out of the context in which you asked it, your question is dumb, since the answer is: print "This is C code" unconditionally
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| The #ifdef is to determine the capabilities of the compiler compiling the code, so to speak. Since in executable form, it doesn't matter if it's compiled using C++ or C. The ifdef should be used as a means of knowing whether a C function or a C++ function calls the said function!
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| The use of _cplusplus in a .cpp file is redundant. This is not how it is normally used. It's normally used from within header files, which are typically named .h whether they are C or C++, and even then, its normal purpose is to turn off name mangling in order to allow C code to make calls to C++ code. The reason it prints "This is C++" is because the code is C++. The fact that it's being called from a C function is irrelevant.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #7 | |
| Registered User Join Date: Jun 2005
Posts: 1,649
| Quote:
Some compilers have dual hats: depending on command line options or other settings, they compile code as C or as C++. With those compilers, you need to pick the right mode (eg via command line settings). As brewbuck noted, some compilers compile as C if the file has a .c extension, and as C++ if the file has a .cpp (or .cc) extension. #ifdef _cplusplus is not checked at run time. It is checked as one of the phases during compilation (specifically, by the preprocessor).
__________________ Right 98% of the time, and don't care about the other 3%. | |
| grumpy is offline | |
| | #8 | |
| Registered User Join Date: Jan 2010
Posts: 238
| Quote:
Right-click the file(s) you want to compile as C code and select properties. Then go to "C/C++" -> "Advanced" -> "Compile As" and select "Compile as C Code" (or just rename the file to something.c) | |
| _Mike is offline | |
| | #9 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Bad idea. Compiling .cpp files as C for some reason will serve to confuse and break compatibility with other compilers. Do the right thing: name it with a .c extension instead. It will compile fine in VS and other compilers.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #10 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,579
| It won't, since it contains C++ constructs; specifically, the 'extern "C"'.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| #ifdef error | Bubba | C++ Programming | 2 | 01-12-2010 07:03 PM |
| Buidl Library with ./configure script | Jardon | C Programming | 6 | 07-24-2009 09:36 AM |
| #ifdef - Can It Be Used With Logic Such as OR / AND? | dedham_ma_man | C Programming | 3 | 04-21-2006 02:57 PM |
| Use of #ifdef for portability - a specific case | hzmonte | C Programming | 7 | 11-03-2005 11:24 PM |
| Difference between ifdef and if defined? | z33z | C++ Programming | 6 | 10-28-2001 11:36 PM |