VC++ resources in a static library

前端 未结 7 1960
天涯浪人
天涯浪人 2020-11-29 21:30

Is it possible to build resources into a static library and reuse them by simply linking with the library?

I\'m primarily thinking about the case where you call a fu

7条回答
  •  既然无缘
    2020-11-29 22:05

    I just went through this with the MS Visual Studio compiler. We were converting some legacy projects from DLLs into static libraries. Several of these DLLs had dialog or string resources embedded in them. I was able to compile the .RC scripts for these DLLs into our main application by including them in the main application's RC script file via the "TEXTINCLUDE" mechanism. I found it easiest to do this by editing the RC file directly, but Visual Studio provides a slightly more "wizardy" mechanism as well. The implementation is most likely different in other compilers.


    To manipulate the main RC script directly:

    .1. In the "2 TEXTINCLUDE" section, include the header file that defines the resource IDs for your library. The syntax is

    2 TEXTINCLUDE 
    BEGIN
        "#include ""my_first_lib_header.h""\r\n"
        "#include ""my_second_lib_header.h""\0" 
    END
    

    .2. In the "3 TEXTINCLUDE" section, include the RC script from your library.

    3 TEXTINCLUDE
    BEGIN
        "#include ""my_first_library.rc""\r\n"
        "#include ""my_second_library.rc""\0"
    END
    

    Steps 3 and 4 should happen automatically, but I found it was more reliable to just enter them myself, rather than depending on Microsoft's resource script compiler to take care of things.

    .3. Add the header file with your libraries resource defines to the read only symbols list. This list is usually near the top of the file.

    #define APSTUDIO_READONLY_SYMBOLS
    #include "my_first_lib_header.h"
    #include "my_second_lib_header.h"
    #undef APSTUDIO_READONLY_SYMBOLS
    

    .4. Include your library's RC script in the APSTUDIO_INVOKED section. This is usually at the bottom of the file.

    #ifndef APSTUDIO_INVOKED
    #include "my_first_library.rc"
    #include "my_second_library.rc"
    #endif 
    

    You can also do all of this automatically through the visual studio IDE, but I found it didn't always apply when I expected it to.

    1. Open the "Resource View" window in Visual Studio.
    2. Right-click on your main application's resource file and choose "Resource Includes..." from the context menu.
    3. In the box labeled "Read-only symbol directives," add the include statements for the .h files that define the resource ID's for your libraries.
    4. In the box labeled "Compile-time directives," add the include statements for your library's .rc script.
    5. Click okay. You may also want to manually trigger the RC script compilation, to make sure it happens.

    If your library's resource script references any files on disk (text files, icons files, etc.), you'll need to make sure that the main application project knows where to find them. You can either copy these files to somewhere your application can find them or you can add an additional include path in the compiler settings.

    To add an additional include path:

    1. Open up the properties dialog for your main application.
    2. Select "Configuration Properties/Resources/General" from the left-hand navigation pane.
    3. In the properties list, Enter any pertinent paths next to "Additional Include Directories."

提交回复
热议问题