target_link_libraries中的PRIVATE INTERFACE PUBLIC

CMake target_link_libraries Interface Dependencies - Stack Overflow
CMake的链接选项:PRIVATE,INTERFACE,PUBLIC - 知乎

If you are creating a shared library and your source cpp files #include the headers of another library (Say, QtNetwork for example), but your header files don't include QtNetwork headers, then QtNetwork is a PRIVATE dependency.

If your source files and your headers include the headers of another library, then it is a PUBLIC dependency.

If your header files other than your source files include the headers of another library, then it is an INTERFACE dependency.

Other build properties of PUBLIC and INTERFACE dependencies are propagated to consuming libraries.

测试PUBLIC

// foo.h
int foo();
struct A {
int num1;
double num2;
};
// foo.cpp
#include "foo.h"
int foo()
{
return sizeof(A);
}
// bar.h
#include "foo.h"
int bar();
// bar.cpp
#include "foo.h"
int bar()
{
return foo() * 10;
}
// main.cpp
#include <iostream>
#include "bar.h"
int main()
{
std::cout << foo() << std::endl;
std::cout << bar() << std::endl;
return 0;
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(foobar)
set(CMAKE_SKIP_RPATH TRUE)
include_directories(${CMAKE_SOURCE_DIR})
add_library(foo SHARED foo.cpp)
add_library(bar SHARED bar.cpp)
target_link_libraries(bar PUBLIC foo)
add_executable(main main.cpp)
target_link_libraries(main bar)
$ export LD_LIBRARY_PATH=$PWD
$ ldd *.so
libbar.so:
libfoo.so => /usr1/tmp/cmake1/build/libfoo.so (0x00007f8657389000)
libfoo.so:
statically linked
$ ldd main
libbar.so => /usr1/tmp/cmake1/build/libbar.so (0x00007f68298cc000)
libfoo.so => /usr1/tmp/cmake1/build/libfoo.so (0x00007f68298c7000)
$ nm -C libbar.so | grep "foo\|bar"
0000000000001119 T bar()
U foo()
$ nm -C main | grep "foo\|bar"
U bar()
U foo()

测试PRIVATE

// foo.h
struct A {
int num1;
double num2;
};
int foo();
// foo.cpp
#include "foo.h"
int foo()
{
return sizeof(A);
}
// bar.h
int bar();
// bar.cpp
#include "foo.h"
int bar()
{
return foo() * 10;
}
// main.cpp
#include <iostream>
#include "bar.h"
int main()
{
std::cout << bar() << std::endl;
return 0;
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(foobar)
set(CMAKE_SKIP_RPATH TRUE)
include_directories(${CMAKE_SOURCE_DIR})
add_library(foo SHARED foo.cpp)
add_library(bar SHARED bar.cpp)
target_link_libraries(bar PRIVATE foo)
add_executable(main main.cpp)
target_link_libraries(main bar)
$ export LD_LIBRARY_PATH=$PWD
$ ldd *.so
libbar.so:
libfoo.so => /usr1/tmp/cmake2/build/libfoo.so (0x00007fcaaa295000)
libfoo.so:
statically linked
$ ldd main
libbar.so => /usr1/tmp/cmake2/build/libbar.so (0x00007fea1beff000)
libfoo.so => /usr1/tmp/cmake2/build/libfoo.so (0x00007fea1baa0000)
$ nm -C libbar.so | grep "foo\|bar"
0000000000001119 T bar()
U foo()
$ nm -C main | grep "foo\|bar"
U bar()

测试INTERFACE

// foo.h
struct A {
int num1;
double num2;
};
int foo();
// foo.cpp
#include "foo.h"
int foo()
{
return sizeof(A);
}
// bar.h
#include "foo.h"
int bar();
// bar.cpp
int bar()
{
return 123 * 10;
}
// main.cpp
#include <iostream>
#include "bar.h"
int main()
{
std::cout << foo() << std::endl;
std::cout << bar() << std::endl;
return 0;
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(foobar)
set(CMAKE_SKIP_RPATH TRUE)
include_directories(${CMAKE_SOURCE_DIR})
add_library(foo SHARED foo.cpp)
add_library(bar SHARED bar.cpp)
target_link_libraries(bar INTERFACE foo)
add_executable(main main.cpp)
target_link_libraries(main bar)
$ export LD_LIBRARY_PATH=$PWD
$ ldd *.so
libbar.so:
statically linked
libfoo.so:
statically linked
$ ldd main
libbar.so => /usr1/tmp/cmake3/build/libbar.so (0x00007f80dd28e000)
libfoo.so => /usr1/tmp/cmake3/build/libfoo.so (0x00007f80dd289000)
$ nm -C libbar.so | grep "foo\|bar"
00000000000010f9 T bar()
$ nm -C main | grep "foo\|bar"
U bar()
U foo()
posted @   devin1024  阅读(107)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示