![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Accessing a C struct from C++
I have a header file shared between some C files and some C++ files, declaring a struct (which is a bunch of entry points into the main application from a DLL) as extern, and the actual definition is in a C source file. Now, the C compiles just fine, but when the C++ compiles (this is all into one output DLL btw) it can't find the struct. I've tried about 349058349587234975^23490578349e34078 different combinations of #ifdef __cplusplus, extern "C", moving the actual definition around, etc...nothing works. Either it's undefined or multiply defined. Can anybody come up with a decent solution?
|
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Can we see some code?
|
|
|
|
|
|
#3 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#4 |
|
Programmer
|
OK, I exaggerated a bit. :p
There's one header (cg_syscalls.h) that is mostly aliasing functions from an ugly "CGAME_IMPORT.R_DrawStretchPic" to the much nicer "trap_DrawStratchPic", where CGAME_IMPORT is the struct of functions imported from the engine. Anyway, the relevant bits of code: cg_syscalls.h: extern cgame_import_t CGAME_IMPORT;
static inline void trap_Print( char *msg ) {
CGAME_IMPORT.Print( msg );
}
static inline void trap_Error( char *msg ) {
CGAME_IMPORT.Error( msg );
}
static inline cvar_t *trap_Cvar_Get( char *name, char *value, int flags, char *description ) {
return CGAME_IMPORT.Cvar_Get( name, value, flags, description );
}cg_syscalls.c: cgame_import_t CGAME_IMPORT;
...
cgame_export_t *GetCGameAPI( cgame_import_t *import )
{
static cgame_export_t globals;
CGAME_IMPORT = *import;
globals.API = CG_API;
globals.Init = CG_Init;
globals.Shutdown = CG_Shutdown;window.cpp (for example): trap_DrawStretchPic(dx, dy - 8, width, 16, "gfx/menu/controls/dialog_title"); trap_DrawString(dx, dy - 8, colorBlack, title); // Main window trap_DrawStretchPic(dx, dy + 8, width, height, "gfx/menu/controls/dialog"); All of the source files for the dll include cg_syscalls.h. It works fine from the C code, but again, it's undefined inside the C++: uicontrol.obj : error LNK2001: unresolved external symbol "struct cgame_import_t CGAME_IMPORT" (?CGAME_IMPORT@@3Ucgame_import_t@@A) window.obj : error LNK2019: unresolved external symbol "struct cgame_import_t CGAME_IMPORT" (?CGAME_IMPORT@@3Ucgame_import_t@@A) referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ) menubar.obj : error LNK2001: unresolved external symbol "struct cgame_import_t CGAME_IMPORT" (?CGAME_IMPORT@@3Ucgame_import_t@@A) Yes, for those of you familiar with modified quake2 engines, it's QFusion-based. ![]() |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
When you include the cg_syscalls.h file from your C++ files, you need to tell the compiler that they contain C declarations, rather than C++.
extern "C"
{
#include "cg_syscalls.h"
} |
|
|
|
|
|
#6 |
|
Programmer
|
Aha...the only thing I didn't try...that'll probably do it.
|
|
|
|
|
|
#7 |
|
Programmer
|
Yep. Fixed it.
Thanks. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|