problem accessing globals in C library

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

problem accessing globals in C library

Post 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.
ivan
Earned a small fee
Earned a small fee
Posts: 17
Joined: Wed Feb 15, 2006 9:40 am
Location: Serbia & Montenegro

Post by ivan »

You may try to define the structures in a header file and include it both in your application and the library.
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

Post 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.
tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post 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
-tiwag
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

Post by Raghu »

This solution is for functions. I want to use global variables and structures across the apllication and library.
tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post 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
-tiwag
wxuser
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Feb 25, 2005 5:45 am
Location: India

Post 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.
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

Post 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.
tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post 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
Last edited by tiwag on Tue Feb 21, 2006 11:01 am, edited 5 times in total.
-tiwag
tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post 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 !
-tiwag
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

Post by Raghu »

Thank a lot
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

equivalent to inet_addr()

Post by Raghu »

Is there any equivalent functions in wxWidgets for inet_addr() and inet_aton() ?
Post Reply