Page 1 of 1

problem accessing globals in C library

Posted: Tue Feb 21, 2006 6:14 am
by Raghu
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.

Posted: Tue Feb 21, 2006 6:29 am
by ivan
You may try to define the structures in a header file and include it both in your application and the library.

Posted: Tue Feb 21, 2006 7:11 am
by Raghu
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.

Posted: Tue Feb 21, 2006 7:31 am
by tiwag
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

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

//put your function declarations here
int my_c_library_function ( int );

#ifdef __cplusplus
}
#endif

Posted: Tue Feb 21, 2006 7:48 am
by Raghu
This solution is for functions. I want to use global variables and structures across the apllication and library.

Posted: Tue Feb 21, 2006 8:01 am
by tiwag
Raghu wrote:This solution is for functions. I want to use global variables and structures across the apllication and library.
then you simply define

Code: Select all

extern struct my_ext_struct;
extern int my_ext_global_int_var;
read this
http://www.gdv.uni-hannover.de/doc/cpp/ ... rames.html

http://www.gdv.uni-hannover.de/doc/cpp/ ... #Heading76

Posted: Tue Feb 21, 2006 8:36 am
by wxuser
If you are getting linker errors, check the order of libraries while linking. Maybe put your lib ahead of all wx related ones.

Posted: Tue Feb 21, 2006 8:58 am
by Raghu
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.

Posted: Tue Feb 21, 2006 10:16 am
by tiwag
Raghu wrote:Is it possible to share structures between a application and library. ...
i'm doing this too and it works without problems
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.h ------------



--- 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;
}

--- ext_lib.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..."));
}
--- wxapp.cpp ------------

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

Posted: Tue Feb 21, 2006 10:32 am
by tiwag
Raghu wrote:... I'm creating the library first where I used extern for the shared structures...
that's your problem, you didn't understand the meaning of extern keyword.

you have to use extern where you want to use an external symbol
and not where you define it !

Posted: Tue Feb 21, 2006 12:08 pm
by Raghu
Thank a lot

equivalent to inet_addr()

Posted: Tue Feb 21, 2006 1:19 pm
by Raghu
Is there any equivalent functions in wxWidgets for inet_addr() and inet_aton() ?