首页 > 其他分享 >target_link_libraries中的PRIVATE INTERFACE PUBLIC

target_link_libraries中的PRIVATE INTERFACE PUBLIC

时间:2023-07-24 23:55:33浏览次数:45  
标签:include bar target int PRIVATE libraries so cpp foo

CMake target_link_libraries Interface Dependencies
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 "bar.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)
$ ldd *.so
libbar.so:
	linux-vdso.so.1 (0x00007ffddc3ed000)
	libfoo.so => /usr1/tmp/cmake1/build/libfoo.so (0x00007f8657389000)
libfoo.so:
	statically linked

$ 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)
$ ldd *.so
libbar.so:
	linux-vdso.so.1 (0x00007ffd6aad7000)
	libfoo.so => /usr1/tmp/cmake1/build/libfoo.so (0x00007fc5f2ffe000)
libfoo.so:
	statically linked

$ 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)
$ ldd *.so
libbar.so:
	statically linked
libfoo.so:
	statically linked

$ nm -C main | grep "foo\|bar"
                 U bar()
                 U foo()

标签:include,bar,target,int,PRIVATE,libraries,so,cpp,foo
From: https://www.cnblogs.com/devin1024/p/17578665.html

相关文章

  • css3之:target伪类
    CSS3target伪类    -------用来匹配URI某一个标志符的目标元素。一般url中包含"#"字符,后面带有一个标志符号,如:"#top" 1、浏览器支持情况:      除了IE6-8,其他浏览器都支持。     http://caniuse.com/#search=%3Atarget  2、使用方式:   //类似于hov......
  • java private变量
    如何实现Java的私有变量作为一名经验丰富的开发者,我很高兴能够教会你如何实现Java中的私有变量。私有变量是指只能在类内部访问的变量,其他类无法直接访问或修改它们。下面是一个简单的步骤表格,展示了整个实现私有变量的流程。步骤描述1创建一个Java类2声明一个私有......
  • ./bin/redis-server: error while loading shared libraries: libssl.so.1.1
    解决"./bin/redis-server:errorwhileloadingsharedlibraries:libssl.so.1.1"的问题作为一名经验丰富的开发者,我将指导你解决这个问题。首先,让我们了解一下整个解决问题的流程。下面是一个简单的步骤表格,将在接下来的文章中详细解释每一步的细节。步骤操作步骤1......
  • bcftools: error while loading shared libraries: libcrypto.so.1.0.0: cannot open
     001、问题[root@PC1home]#bcftoolsbcftools:errorwhileloadingsharedlibraries:libcrypto.so.1.0.0:cannotopensharedobjectfile:Nosuchfileordirectory 002、解决方法[root@PC1home]#find/-userroot-name"libcrypto.so.*"##查找库文......
  • You can’t specify target table ‘aaa′ for update in FROM clause
    Youcan’tspecifytargettable‘aaa′forupdateinFROMclause使用MySQL执行DELETEFROM时,若子查询的FROM子句和更新/删除对象使用同一张表,会出现错误。针对“同一张表”这个限制,撇开效率不谈,多数情况下都可以通过多加一层SELECT别名表来变通解决,如下:DELETEFRO......
  • springboot元注解@Target@Retention@Documented
        @Target(ElementType.METHOD)是一个元注解,用来标注注解的作用目标。这里的@Target(ElementType.METHOD)表示该自定义注解可以用于方法上。@Retention(RetentionPolicy.RUNTIME)是一个元注解,用来标注注解的保留策略。这里的@Retention(RetentionPolicy.RUNTIME)表......
  • 轻松理解Java中的public、private、static和final
    一、概念1、public和private两个都是访问权限修饰符,用于控制外界对类内部成员的访问。public:表明对象成员是完全共有的,外界可以随意访问。用public修饰的数据成员、成员函数是对所有用户开放的,所有用户都可以直接进行调用。private:表明对象成员是完全私有的,不容许外界的任何......
  • QSqlDatabasePrivate::removeDatabase: connection ‘myConnection’ is still in use
    1.解决QSqlDatabasePrivate::removeDatabase:connection‘myConnection’isstillinuse,allquerieswillceasetowork的问题该问题主要是因为没有关闭之前的数据库连接,然后又需要创建新的数据库连接导致。解决方案:必须释放该连接的所有查询,即删除所有与该连接有关的quer......
  • Could not find compile target android-33 for modules :app
         检查一下这些地方......
  • Makefile:162:recipe for target ‘xxx.o‘ failed!
    Q:使用makefile对工程进行编译的时候,出现指定报错:Makefile:162:recipefortarget‘xxx.o‘failed!A:该问题报错的意思是缺少依赖。根本问题是由于某个错误,导致过程文件xxx.o无法正常编译成功(makefile中最终目标文件的编译需要依赖过程目标文件xxx.o,而过程目标文件xxx.......