Jump to content
  • Advertisement
Sign in to follow this  
Calin

Linker error when porting C code to C++

This topic is 4620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 __cplusplus
extern "C" {
#endif

... Real header stuff ...

#ifdef __cplusplus
};
#endif

#endif

Share this post


Link to post
Share on other sites
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 __cplusplus
extern "C" {
#endif

...

#ifndef __cplusplus
}
#endif


[edit: beaten. Too slooooooow]

HTH,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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} ?

Share this post


Link to post
Share on other sites
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 }

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!