|
Richard MacCutchan wrote: You can write pure C in a .cpp source file.
But then your code won't be compiled as C code, in the sense that it is not pure C anymore (you'll be able to declare variables everywhere in a function for instance). If you specify the extension as .c, your file will be compiled as pure C code.
|
|
|
|
|
Cedric Moonen wrote: If you specify the extension as .c, your file will be compiled as pure C code.
If you really like to be a dinosaur
It's time for a new signature.
|
|
|
|
|
Awww... I am a dinosaur then
I had to do that for a project a couple of weeks ago. But I have to say that moving from C++ to C is really horrible.
|
|
|
|
|
or don't like to carry a big run-time library you don't need, e.g. in embedded systems.
|
|
|
|
|
Luc Pattyn wrote: I typically also have to change the settings about precompiled headers.
And say the compiler to use C. On the project properties set "Compile as C".
Luc Pattyn wrote: I wish there were an easier way, but I don't see one.
Much easier way is to create an empty project and add your files. There change the extension when you add.
Best wishes,
Navaneeth
|
|
|
|
|
N a v a n e e t h wrote: set "Compile as C".
I never did that, it seems to know from the extension (as it should IMO).
N a v a n e e t h wrote: create an empty project
ah. of course. I'll have to remember that. thanks.
|
|
|
|
|
Luc Pattyn wrote: it seems to know from the extension (as it should IMO).
I don't think so. On default settings it uses C++ compiler to compile the code irrespective of what extension it is. You can see this from the output window. This will work because most of the C code is also valid on C++. When you set the "Compile as C" option, you can see adiitional compiler flag getting added to the compile command and it defaults to the C89 standard.
Best wishes,
Navaneeth
|
|
|
|
|
I now located the "compile as C" switch (project Properties/Configuration Properties/CC++/Advanced); I'm pretty sure I've never used it before. I will experiment next time I need some native code stuff. Thanks.
|
|
|
|
|
I just discovered Visual Studio 2008 Express Edition applies /TC (i.e. Compile as C code) for a C file inside a C++ project (which has the setting /TP, i.e. Compile as C++ code). So there is no need to change the setting provided the extension is correct.
|
|
|
|
|
You are correct. It does automatically. Thanks for pointing that out!
Best wishes,
Navaneeth
|
|
|
|
|
Luc Pattyn wrote: ...it seems to know from the extension (as it should IMO).
It always has for me. I've never changed compiler options when switching between C and CPP files.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
No need to switch settings, see my 2 replies to nananeeth.
|
|
|
|
|
PC17 wrote: What project i must open?
Just start a new project and select either Console or Windows app, VS will create a skeleton project for you to add your own code and will automatically select the correct compiler options. If you do not know how to use Visual Studio then take a look at some of the self study guides on the start page.
It's time for a new signature.
|
|
|
|
|
|
Walkthrough: Compiling a C Program
Like this link works the compiller but so i must write the code with notepad or an editor.
But how can i make a new projekt in VS10 and compile it there, so as with C#.
What i must do? I open a new project then ?
|
|
|
|
|
Choose "Empty project". When adding files, specify extension as C (actually extension doesn't matter). Now on the project properties, choose "Compile as C" option.
Best wishes,
Navaneeth
|
|
|
|
|
Message Closed
modified 2-Apr-21 5:21am.
|
|
|
|
|
"Project" menu -> Properties(last menu) -> C/C++ -> Advanced -> In right side "Compile As" -> "Compile as C Code(/TC)".
--
"Programming is an art that fights back!"
|
|
|
|
|
N a v a n e e t h wrote: actually extension doesn't matter
I don't know for the 2010 version, but for the 2005 version, if your file has a c extension, it will be automatically compiled as C code. So, the extension does make a difference.
|
|
|
|
|
New Project -> Visual C++ -> Empty Project
In Solution Explorer window, right click & select "Source Files" -> "Add" -> "New Item"
On left side of the "Add new item" dialog, select "Code"
In the file name field type "YourFileName.c" and click "Add"
Write some code in the file like below and press "Ctrl + F5", you can see the output:
<br />
#include "stdio.h"<br />
<br />
int main()<br />
{<br />
int nNum = 10;<br />
char cChar = 'a';<br />
printf("%d", nNum);<br />
printf("\n");<br />
printf("%c", cChar);<br />
printf("\n");<br />
<br />
return 0;<br />
}<br />
Regards
--
"Programming is an art that fights back!"
|
|
|
|
|
I'm using a CStatic as a 'draw' window in a dialog. To get the extents of the window I'm using:-
CDC *pDC;
CWnd *wnd;
CRect R;
pDC = AWindow.GetDC();
wnd = pDC->GetWindow();
wnd->GetClientRect(&R);
This works fine in Debug but craps out in Release, at the moment I'm stumped, I've narrowed it down to being wnd=pDC->GetWindow() as the problem, or at least thats where is stops, any ideas?
|
|
|
|
|
Where is that code ?
If it is in the OnPaint method, you need to use CPaintDC .
to get the rect of the static control, can't you simply use this->GetWindowRect(rect) ?
Watched code never compiles.
|
|
|
|
|
Forgot about this->GetWindowRect(), now works
Don't know why GetWindow didn't work though...
But thanks
|
|
|
|
|
I have multiple derived classes and a function that accepts their (abstract) base ptr.
Right now I have a lot of code like this in my function:
void foo(BaseClass* p)
{
if (DerivedClass1 *p1 = dynamic_cast<DerivedClass1 *>(p))
{
...;
}
else if(DerivedClass2 *p2 = dynamic_cast<DerivedClass2 *>(p))
{
...;
}
}
I want to replace it with a switch if possible:
switch (p.type_id)
{
case DerivedClass1.type_id: ...; break;
case DerivedClass2.type_id: ...; break;
}
is that possible to do?
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
Derived class ids
#define ID_DER1 01
#define ID_DER2 02
in base class, add a pure virtual function
virtual int GetTypeId() = 0;
its implementation in derived classes
int DerivedClass1::GetTypeId()
{ return ID_DER1; }
int DerivedClass2::GetTypeId()
{ return ID_DER2; }
and the foo function looks like
void foo(BaseClass* p)
{
switch (p->GetTypeID())
{
case ID_DER1: printf("der1");
break;
case ID_DER2: printf("der2");
break;
}
}
|
|
|
|