Home » U++ TheIDE » U++ TheIDE: Compiling, Linking, Debugging of your packages » Mixing C and C++ files (windows with MinGW compiler)
Mixing C and C++ files (windows with MinGW compiler) [message #10129] |
Thu, 21 June 2007 19:44 |
malaugh
Messages: 7 Registered: June 2007
|
Promising Member |
|
|
I am a newbee. Sorry for the basic questions.
If I make a program that calls some old C code from main.cpp in my Ultimate++ program, an get "undefined symbol" link errors.
I wrote test code shows my problem
---------- main.cpp ------------------------------
void __cdecl GetHello(char *Text);
Hello::Hello()
{
char HelloText[12];
CtrlLayout(*this, "Window title");
GetHello(HelloText);
lblHello = HelloText;
}
GUI_APP_MAIN
{
Hello().Run();
}
------------------------------------------------------
------------------- HelloText.c ----------------------
void __cdecl GetHello(char *Text)
{
strcpy(Text, "Hello, World");
}
---------------------------------------------------------
I have main.c and HellText.c in my package. When I try to compile, I get
1 undefined symbol(s): GetHello(char*)(referenced from.... linking aborted due to fatal errors
If I move the GetHello function to main.cpp, the program compiles without error.
If I rename HelloText.c to HelloText.cpp, the program also compiles without error.
What am I doing wrong???
|
|
|
Re: Mixing C and C++ files (windows with MinGW compiler) [message #10150 is a reply to message #10129] |
Fri, 22 June 2007 07:27 |
waxblood
Messages: 95 Registered: January 2007
|
Member |
|
|
In a cpp file you have to wrap all C functions prototypes (or include) statements with this block:
extern "C" {
}
in your example the correct form is:
---------- main.cpp ------------------------------
extern "C" {
void __cdecl GetHello(char *Text);
}
Hello::Hello()
{
char HelloText[12];
CtrlLayout(*this, "Window title");
GetHello(HelloText);
lblHello = HelloText;
}
GUI_APP_MAIN
{
Hello().Run();
}
Ciao,
David
|
|
|
Goto Forum:
Current Time: Sat Apr 20 01:44:31 CEST 2024
Total time taken to generate the page: 0.07027 seconds
|