我有一个主程序A和一个包含的共享库libB.so。我之所以创建库B是因为它所利用的库与程序A所利用的库同时包含时存在冲突。我想确保程序A的编译器不会尝试包含库B使用的任何库。
但是,在当前设置下,程序A的编译器抱怨找不到库B所包含的库(在本例中为库D)。关于如何修改此代码以防止库B的依赖项也被程序A加载的任何想法?
a.cpp:
#include "b.h"
#include "c.h"
//c.h is a library that does not work when library d (which is in b.h) is included
int main() {
b object;
object.print();
}
b.cpp
#include "b.h"
#include "d.h"
//d.h is a library that does not work when library c (which is in a.h) is included
b::b() {}
void b::print() { std::cout << "Hello"; }
b.h
#include "d.h"
#include <iostream>
class b {
public:
b();
void print();
};
程序A的CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
PROJECT ( a )
list( APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} )
include( FindPkgConfig )
find_package( b REQUIRED )
find_package( c REQUIRED )
include_directories( ${B_INCLUDE_DIR} ${C_INCLUDE_DIR} )
target_link_libraries( a ${B_LIBRARY} ${C_LIBRARY} )
add_executable( a ${B_LIBRARY} ${C_LIBRARY} )
程序B的CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
PROJECT ( b )
list( APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR} )
include( FindPkgConfig )
find_package( d REQUIRED )
include_directories( ${D_INCLUDE_DIR} )
target_link_libraries( b ${D_LIBRARY} )
add_library( b SHARED ${D_LIBRARY} )
感谢您的帮助!