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
int foo () ;
struct A {
int num1;
double num2;
};
#include "foo.h"
int foo ()
{
return sizeof (A);
}
#include "foo.h"
int bar () ;
#include "foo.h"
int bar ()
{
return foo () * 10 ;
}
#include <iostream>
#include "bar.h"
int main ()
{
std::cout << foo () << std::endl;
std::cout << bar () << std::endl;
return 0 ;
}
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
struct A {
int num1;
double num2;
};
int foo () ;
#include "foo.h"
int foo ()
{
return sizeof (A);
}
int bar () ;
#include "foo.h"
int bar ()
{
return foo () * 10 ;
}
#include <iostream>
#include "bar.h"
int main ()
{
std::cout << bar () << std::endl;
return 0 ;
}
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
struct A {
int num1;
double num2;
};
int foo () ;
#include "foo.h"
int foo ()
{
return sizeof (A);
}
#include "foo.h"
int bar () ;
int bar ()
{
return 123 * 10 ;
}
#include <iostream>
#include "bar.h"
int main ()
{
std::cout << foo () << std::endl;
std::cout << bar () << std::endl;
return 0 ;
}
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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现