Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 13th, 2006, 3:17 PM   #1
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
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?
niteice is offline   Reply With Quote
Old Jan 13th, 2006, 4:16 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Can we see some code?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 13th, 2006, 4:17 PM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Ooble
Can we see some code?
Yeah, because at an amount of 349058349587234975^23490578349e34078 possibilities we don't know which one you missed
Polyphemus_ is offline   Reply With Quote
Old Jan 13th, 2006, 6:31 PM   #4
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
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 );
}
etc.

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;
etc.

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.
niteice is offline   Reply With Quote
Old Jan 13th, 2006, 7:57 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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"
}
This tells the compiler not to "mangle" the names, so the linker can then find the functions or variables.
The Dark is offline   Reply With Quote
Old Jan 13th, 2006, 8:08 PM   #6
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
Aha...the only thing I didn't try...that'll probably do it.
niteice is offline   Reply With Quote
Old Jan 14th, 2006, 8:06 AM   #7
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
Yep. Fixed it. Thanks.
niteice is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC