problem accessing globals in C library
problem accessing globals in C library
I have defined some global structures in my wxWidgets application. I need to access those structures in a C library(.lib) which is linked to wxWidget application. I tried defining the structure as extern in the C library, but doesn't seems to work. Can anybody help me out.
I tried, but it gives unresolved external symbols while linking. Do I need to make any changes in Comilation flags.
I defined the structure in a header file and included it from the application and library. I declared the structure in wxwidgets application and made the structure extern in library. But it gives unresolved external symbols while linking.
I defined the structure in a header file and included it from the application and library. I declared the structure in wxwidgets application and made the structure extern in library. But it gives unresolved external symbols while linking.
it is because of C and CPP are using different names for functions (name-mangling)
use extern "C" so that the compiler knows which name-mangling method to use
use extern "C" so that the compiler knows which name-mangling method to use
Code: Select all
#ifdef __cplusplus
extern "C" {
#endif
//put your function declarations here
int my_c_library_function ( int );
#ifdef __cplusplus
}
#endif
-tiwag
then you simply defineRaghu wrote:This solution is for functions. I want to use global variables and structures across the apllication and library.
Code: Select all
extern struct my_ext_struct;
extern int my_ext_global_int_var;
http://www.gdv.uni-hannover.de/doc/cpp/ ... rames.html
http://www.gdv.uni-hannover.de/doc/cpp/ ... #Heading76
-tiwag
I am able to create library(.lib) without any problem. I'm creating the library first where I used extern for the shared structures. When I link the library with my wxwidgets application, I'm getting the unresolved symbol.
Is it possible to share structures between a application and library. I'm planning to pass the structure through some functions between the two. But the problem is I'm passing a huge structure, which I didn't wanted.
Is it possible to share structures between a application and library. I'm planning to pass the structure through some functions between the two. But the problem is I'm passing a huge structure, which I didn't wanted.
i'm doing this too and it works without problemsRaghu wrote:Is it possible to share structures between a application and library. ...
i'm using wx262 and gcc 3.4.4
just be shure that you've defined the variables you want to share
as global and NOT "static" !!!
--- ext_lib.h ------------
Code: Select all
/*
* ext_lib.h
*
* static c-library
*/
#ifdef __cplusplus
extern "C"
{
#endif
extern int g_lib_var;
void set_g_lib_var(int val);
struct g_lib_struct_s
{
int a;
int b;
int c;
};
extern struct g_lib_struct_s g_lib_struct;
void set_g_lib_struct(int a, int b, int c);
#ifdef __cplusplus
}
#endif
--- ext_lib.c ------------
Code: Select all
/*
* ext_lib.c
*
* static c-library
*/
#include "ext_lib.h"
int g_lib_var;
void set_g_lib_var(int val) {
g_lib_var = val;
}
struct g_lib_struct_s g_lib_struct;
void set_g_lib_struct(int a, int b, int c) {
g_lib_struct.a = a;
g_lib_struct.b = b;
g_lib_struct.c = c;
}
in the file where you want to access them,
include the header and use them:
--- wxapp.cpp ------------
Code: Select all
#include "ext_lib.h"
void MyFrame::OnAbout(wxCommandEvent& event)
{
set_g_lib_var(123);
set_g_lib_struct(4,5,6);
wxString msg;
msg.Printf(_T("g_lib_var = %d \n")
_T("g_lib_struct = { %d, %d, %d } \n"),
g_lib_var,
g_lib_struct.a,
g_lib_struct.b,
g_lib_struct.c );
wxMessageBox(msg, _("Welcome to..."));
}
and then link your main app with the ext_lib you've created
be shure you give the linker also the correct path to your lib,
so that it can find it.
[edit]
header file added
you can download the complete files from here :
http://www.tiwag.front.ru/devel/wx_ext_lib.zip
it includes also a CodeBlocks project file
CodeBlocks you can download from here :
http://forums.codeblocks.org/index.php?topic=2406.0
Last edited by tiwag on Tue Feb 21, 2006 11:01 am, edited 5 times in total.
-tiwag
equivalent to inet_addr()
Is there any equivalent functions in wxWidgets for inet_addr() and inet_aton() ?