macOS静态库与动态库

Static Library(.a)

Static libraries allow an application to load code into its address space at compile time.This results in a larger size on disk and slower launch times. Because the library’s code is added directly to the linked target’s binary, it means that to update any code in the library, the linked target would also have to be rebuilt.

static_lib

Dynamic Library(.dylib)

Dynamic libraries allow an application to load code into its address space when it’s actually needed at run time. Because the code isn’t statically linked into the executable binary, there are some benefits from loading at runtime. Mainly, the libraries can be updated with new features or bug-fixes without having to recompile and relink executable. In addition, being loaded at runtime means that individual code libraries can have their own initializers and clean up after their own tasks before being unloaded from memory

dynamic_lib

CMake favours passing the full path to link libraries

Assuming libfoo.a is in ${CMAKE_SOURCE_DIR}, to link the library using:

1
2
add_executable(main main.cpp)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/libfoo.a)

The same for dynamic library:

1
2
3
4
add_executable(main main.cpp)
target_link_libraries(main
/usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshTools.6.3.dylib
/usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshCore.6.3.dylib)

Reference