简体   繁体   中英

How to build static and shared library at the same time through cmake

I am learning cmake on windows.
I read and run this example so that I can build both static and shared libraries from one visual studio project.
I found this command:

add_library(math SHARED ${MATH_SOURCES} ${SIMPLE_FUNCTION_SOURCES} ${ADVANCED_FUNCTION_SOURCES})

According to my understanding, this command is building shared library. Also, from here , if I want to build static and shared library, I have to do it by two projects.

So, why the first example I used can build both libraries from one Visual Studio project?

Edit
Thanks for Florian , the key point is in MathExports.h . To let people easier to see, I put the file here:

#ifndef MathExports_h
#define MathExports_h

#ifdef _WIN32
    #ifdef math_EXPORTS
        #define  MATH_EXPORT __declspec( dllexport )
    #else
        #define  MATH_EXPORT __declspec( dllimport )
    #endif
#else
    #define    MATH_EXPORT
#endif

#endif // MathExports_h

The linked example doesn't build a standalone static math library. It does have "exports" (see MathExports.h ) and therefore it's generating an additional import .lib (which references the build shared library).

This import library is used when you are "linking" against the DLL.

You can use

dumpbin /symbols math.lib

to see the references in math.lib to math.dll .

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM