Linker error when porting C code to C++

Started by
9 comments, last by Alpha_ProgDes 18 years, 1 month ago
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.

My project`s facebook page is “DreamLand Page”

Advertisement
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?
Thanks guys, it worked [smile]

My project`s facebook page is “DreamLand Page”

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]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by LessBread
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]

shouldn't that be: "bool" {"true", "false"} and BOOL {TRUE, FALSE} ?

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
Quote:Original post by LessBread
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]

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 }

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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]

This topic is closed to new replies.

Advertisement