Linker error when porting C code to C++
I'm trying to port some C code snippets to my C++ game.
The problem is this linker error:
error LNK2019: unresolved external symbol "int __cdecl init_raw_mouse(int,int,int)" (?init_raw_mouse@@YAHHHH@Z) referenced in function _WinMain@16
The function is declared and defined in a 'h' and a 'c' file.
declaration: BOOL init_raw_mouse(BOOL, BOOL, BOOL);
definition: BOOL init_raw_mouse(BOOL in_include_sys_mouse, BOOL in_include_rdp_mouse, BOOL in_include_individual_mice){ ...
Why does the compiler expects a 'int' instead of 'BOOL'?
Separately these .h and .c files compile just fine.
Is the definition in a C file and the call in a C++ file (or the other way round?). If so, you have to use extern "C" before the C++ declaration so it isn't name mangled. This is usually done in header files like this:
#ifndef HEADER_SYMBOL#define HEADER_SYMBOL#ifdef __cplusplusextern "C" {#endif... Real header stuff ...#ifdef __cplusplus};#endif#endif
Quote:Original post by Calin
I'm trying to port some C code snippets to my C++ game.
The problem is this linker error:
error LNK2019: unresolved external symbol "int __cdecl init_raw_mouse(int,int,int)" (?init_raw_mouse@@YAHHHH@Z) referenced in function _WinMain@16
The function is declared and defined in a 'h' and a 'c' file.
declaration: BOOL init_raw_mouse(BOOL, BOOL, BOOL);
definition: BOOL init_raw_mouse(BOOL in_include_sys_mouse, BOOL in_include_rdp_mouse, BOOL in_include_individual_mice){ ...
Why does the compiler expects a 'int' instead of 'BOOL'?
Separately these .h and .c files compile just fine.
BOOL is a typedef to some kind of in (maybe unsigned, I don't remember). "bool" is the C++ type. They are different and should not be mixed ("BOOL b = false;" is evil).
Try to suround your function definitions with
#ifndef __cplusplusextern "C" {#endif...#ifndef __cplusplus}#endif
[edit: beaten. Too slooooooow]
HTH,
As to why it expects int, that's because BOOL isn't a real type - it's a typedef for C code. It gets worse: some 'standard' functions declared to return BOOL will return integer values other than 0 or 1.
1° There is no problem with compiling, since you get a linker error.
2° BOOL is defined as typedef int BOOL.
3° Are you linking the .obj file of the function with the .obj file of Winmain?
2° BOOL is defined as typedef int BOOL.
3° Are you linking the .obj file of the function with the .obj file of Winmain?
Quote:Original post by Calin
Why does the compiler expects a 'int' instead of 'BOOL'?
BOOL is not an intrinsic data type in C. It's simply a typedef of an int.
typedef int BOOL;
Since you're porting the code, you might consider substituting "bool" for BOOL and "true" and "false" for TRUE and FALSE in order to bring the code into line with C++ conventions.
@ Emmanuel Deloget - not as slow as me! [smile]
Quote:Original post by LessBreadQuote:Original post by Calin
Why does the compiler expects a 'int' instead of 'BOOL'?
BOOL is not an intrinsic data type in C. It's simply a typedef of an int.
typedef int BOOL;
Since you're porting the code, you might consider substituting "bool" for BOOL and "true" and "false" for TRUE and FALSE in order to bring the code into line with C++ conventions.
@ Emmanuel Deloget - not as slow as me! [smile]
shouldn't that be: "bool" {"true", "false"} and BOOL {TRUE, FALSE} ?
Quote:Original post by Alpha_ProgDesQuote:Original post by LessBreadQuote:Original post by Calin
Why does the compiler expects a 'int' instead of 'BOOL'?
BOOL is not an intrinsic data type in C. It's simply a typedef of an int.
typedef int BOOL;
Since you're porting the code, you might consider substituting "bool" for BOOL and "true" and "false" for TRUE and FALSE in order to bring the code into line with C++ conventions.
@ Emmanuel Deloget - not as slow as me! [smile]
shouldn't that be: "bool" {"true", "false"} and BOOL {TRUE, FALSE} ?
The quotes are simply to indicate that those aren't words in the ordinary sense. Emmanuel useds quotes too, probably for the same reason.
It seems to me that it should be
bool { true, false } substituted for BOOL { TRUE, FALSE }
Indeed, I used quotes to say "look, I'm not typing BOOL nor Bool nor bOOl but "bool"". Er... I just wanted to insist that the C++ keyword was "bool".
Silly me. I added those damn quotes again [smile]
Silly me. I added those damn quotes again [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement