开发者

Link a Library within a Library with externs?

开发者 https://www.devze.com 2023-01-27 17:28 出处:网络
Im creating a library to interface into drawing a specific file type. This library needs to pull in a 3rd party library to do some processing on this specific file type. This 3rd party lib requires th

Im creating a library to interface into drawing a specific file type. This library needs to pull in a 3rd party library to do some processing on this specific file type. This 3rd party lib requires the users of it to create some extern functions that it uses to make data types on target hardware.

So, it looks like this:

----------------
|              | 
| 3rd Party    | 
|   Lib        | 
|              | 
----------------
      |
      |
     \/
---------------
|             | 
|  My Lib     | 
---------------
|  externs    | 
---------------
      |
      |
     \/
---------------
|             | 
|  My App     | 
|             | 
|             | 
---------------

My App calls My Lib and says "draw me file X", My Lib does a bunch of junk eventually calling 3rd Party Lib and says "process file X". 3rd Party Lib requires several externs to be declared that it uses.

I made all these externs inside My Lib. My problem is, when I link My App, its telling me undefined references to all the extern functions. I can't figure out why. Heres one of the externs inside My Lib to be called by 3rd Party Lib:

// inside typeConversions.c

extern int CreateShort(short* shortList, int numShorts)
{
    int i;

    for(i = 0; i < numShorts; i++)
    {
        BYTE_SWAP_SHORT(shortList[i]);
    }

    return ERROR_OK;
}

When I link My App I get (开发者_开发知识库among several other similar errors):

open.cpp: undefined reference to `CreateShort'

Where open.cpp is inside the 3rd Party Lib.

I have both My Lib and 3rd Party Lib included in My App. Ideally, I want to not expose users of My Lib to 3rd Party Lib, so for example My App would not have to include 3rd Party Lib at all, they dont know it even exists. All that should be exposed is My Lib. Is this possible? Anyone know how to fix my linking problem?


1) Look up "calling conventions" in your compiler and linker documentation. You most likely have a mismatch.

2) In C, (not C++) The extern keyword should be used in the header, not on the function definition. Make sure you include the header in all files that use the declarations, including the .C file that implements what the header declares.

    // This goes in a header file
    extern int CreateShort(short* shortList, int numShorts)

    // This goes in a .c file.
    int CreateShort(short* shortList, int numShorts)
    {
        ...
        return ERROR_OK;
    }

3) If you are using C++ to write the functions that the 3rd party lib calls, and the 3rd party lib expects things to work like C, you'll have to use extern "C" to prevent name mangling.

    // This goes in a header file
    extern "C" int CreateShort(short* shortList, int numShorts)
    // You can also wrap it in a block:
    // extern "C" {
    // int someFunc1( int x );
    // int someFunc2( int y );
    // }

    // This goes in a .cpp file.
    extern "C" int CreateShort(short* shortList, int numShorts)
    {
        ...
        return ERROR_OK;
    }


Seems like my problem was that I didnt have the header file containing my externs "visible" enough for the linker to see when linking the app. I have several folders in my library workspace, moving the header file to the top of this heirarchy fixed my issue. Weird!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号