CPP 共享库的创建与使用

CPP 共享库的创建与使用
Qusetion: 大量开源软件如何有效为己所用,站在巨人的肩膀上,前提有两个,即理解开源库的结构与信息传递, MTT(message Transform &  Transfer ); 同时如何有效使用也是需要解决的问题。
Example: 

libBondGraph作为库的使用
* 生成库

*******************************************************************************
e_minimum_required(VERSION 2.4)

project (LIBBONDGRAPH)
set(LIBBONDGRAPH_VERSION 1.0.0)

set( CMAKE_VERBOSE_MAKEFILE ON )

option( USE_RKF "Build the project using Runge-Kutta Fehlberg" OFF )
option( USE_RKFVS "Build the project using Runge-Kutta Fehlberg integration methods with variable time step" OFF )
option( DEBUG_EQN "Build the project and enable equation debugging output" OFF )
option( DEBUG_STATEEQN "Build the project and enable equation debugging output" OFF )

set( PROJECTSROOT ${CMAKE_SOURCE_DIR} CACHE PATH "Path to the root of the sources for all subproject" )

set( PACCSOURCE ${PROJECTSROOT}/PACC CACHE PATH "PACC headers" )
set( PACCBUILD ${PROJECTSROOT}/PACC/build CACHE PATH "PACC build"  )

set( SIMULATIONSOURCE ${PROJECTSROOT}/Source/Simulation CACHE PATH "Simulation headers" )

if( USE_RKF )
    if( USE_RKFVS )
        add_definitions(-DUSE_RKFVS)
    else( USE_RKFVS )
        add_definitions(-DUSE_RKF)
    endif( USE_RKFVS )
endif( USE_RKF )

if( DEBUG_EQN )
    add_definitions(-DDEBUG_EQN)
endif( DEBUG_EQN )

if( DEBUG_STATEEQN )
    add_definitions(-DDEBUG_STATEEQN)
endif( DEBUG_STATEEQN )

add_subdirectory (Source)
add_subdirectory (Examples)
*******************************************************************************

set( CMAKE_CXX_FLAGS -fvisibility-inlines-hidden )

set( BONDGRAPH_INCLUDE_DIR ${PROJECTSROOT}/Source/libBondGraph CACHE PATH "libBondGraph headers" )
set( BONDGRAPH_LIB_DIR ${PROJECTSROOT}/build/Source/libBondGraph CACHE PATH "libBondGraph build"  )

option( WITH_GSL "Build the project using GNU Scientific Library" ON )
if( WITH_GSL )
    set(GSL_DIR "$ENV{HOME}/local")
    set(GSL_INCLUDE_DIR "${GSL_DIR}/include")
    set(GSL_LIB_DIR "${GSL_DIR}/lib")
    add_definitions(-DHAVE_GSL)
    set( PACC_LIBS  gsl gslcblas)
    link_directories( ${GSL_LIB_DIR} )
    include_directories( ${GSL_INCLUDE_DIR} )
endif( WITH_GSL )


include_directories( $ENV{HOME}/local/include /sw/include ${BONDGRAPH_INCLUDE_DIR} ${PACCSOURCE} ${SIMULATIONSOURCE} )
link_directories( $ENV{HOME}/local/lib /sw/lib ${BONDGRAPH_LIB_DIR} ${PACCBUILD} )

set( Examples_LIBS pacc ${PACC_LIBS} BondGraph graph gvc m)

set ( Example1_SRCS
    Example1.cpp
)

set ( Example2_SRCS
    Example2.cpp
)

set ( Example3_SRCS
    Example3.cpp
)

set ( DCDCBoost_SRCS
    DCDCBoostMain.cpp
    DCDCBoostLookaheadController.cpp
    LookaheadController.cpp
    SimulationCase.cpp
)


add_executable (Example1 ${Example1_SRCS} )
target_link_libraries(Example1 ${Examples_LIBS})

add_executable (Example2 ${Example2_SRCS} )
target_link_libraries(Example2 ${Examples_LIBS})

add_executable (Example3 ${Example3_SRCS} )
target_link_libraries(Example3 ${Examples_LIBS})

add_executable (DCDCBoost ${DCDCBoost_SRCS} )
target_link_libraries(DCDCBoost ${Examples_LIBS})

_______________________________________________________________________________


if(COMMAND cmake_policy)
           cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)


project(PACC C CXX)
set(PACC_VERSION 1.2.1)

set( CMAKE_VERBOSE_MAKEFILE ON )

option( WITH_GSL "Build the project using GNU Scientific Library" ON )
if( WITH_GSL )
    set(GSL_DIR "$ENV{HOME}/local")
    set(GSL_INCLUDE_DIR "${GSL_DIR}/include")
    set(GSL_LIB_DIR "${GSL_DIR}/lib")
    add_definitions(-DHAVE_GSL)
    set( PACC_LIBS  gsl gslcblas)
    link_directories( ${GSL_LIB_DIR} )
    include_directories( ${GSL_INCLUDE_DIR} )
endif( WITH_GSL )


file(GLOB_RECURSE PACC_SRC  PACC/*.cpp)
file(REMOVE_RECURSE PACC_SRC  PACC/SVG/*.cpp)

file(GLOB PACC_HEADERS         PACC/*.hpp)
file(REMOVE_RECURSE PACC_HEADERS         PACC/SVG/*.hpp)
file(REMOVE PACC_HEADERS         PACC/SVG.hpp)

file(GLOB PACC_MATH_HEADERS PACC/Math/*.hpp)
file(GLOB PACC_UTIL_HEADERS PACC/Util/*.hpp)
file(GLOB PACC_XML_HEADERS   PACC/XML/*.hpp)
file(GLOB PACC_SOCKET_HEADERS PACC/Socket/*.hpp)
file(GLOB PACC_THREADING_HEADERS PACC/Threading/*.hpp)

# Include headers required
include_directories(${CMAKE_HOME_DIRECTORY})

add_library(pacc SHARED ${PACC_SRC}) //注意生成共享动态库
add_dependencies(pacc pthread ${PACC_LIBS} )
target_link_libraries(pacc pthread ${PACC_LIBS})
set_target_properties(pacc PROPERTIES VERSION ${PACC_VERSION})
set_target_properties(pacc PROPERTIES LINKER_LANGUAGE CXX)


# Install dependencies
install(FILES ${PACC_HEADERS} DESTINATION include/PACC)
install(FILES ${PACC_MATH_HEADERS} DESTINATION include/PACC/Math)
install(FILES ${PACC_UTIL_HEADERS} DESTINATION include/PACC/Util)
install(FILES ${PACC_XML_HEADERS} DESTINATION include/PACC/XML)
install(FILES ${PACC_SOCKET_HEADERS} DESTINATION include/PACC/Socket)
install(FILES ${PACC_THREADING_HEADERS} DESTINATION include/PACC/Threading)

# Install targets
install(TARGETS pacc    DESTINATION lib)


&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

** 安装库
 build, sudo make install
* 使用库
** 确定头文件路径
-I /usr/
** 库链接
 sudo ln -s /usr/local/lib/libpacc.so.1.2.1 /usr/lib

ldd outTestlibPACC 
    linux-vdso.so.1 (0x00007fff8eff6000)
    libpacc.so.1.2.1 => /lib/libpacc.so.1.2.1 (0x00007f2763691000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f27634af000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2763494000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f27632a2000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f276327f000)
    libgsl.so.23 => /lib/x86_64-linux-gnu/libgsl.so.23 (0x00007f2763003000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2762eb2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f2763776000)
    libgslcblas.so.0 => /lib/x86_64-linux-gnu/libgslcblas.so.0 (0x00007f2762e70000)
** 使用
g++ -o outTestlibPACC paccEnvTest1.cpp -I /usr/local/include -lpacc -std=c++11

Principle:
生成库放到适当的位置,系统要能找到, 缺省的放到了 /usr/local/include/pacc, /usr/local/lib/ 
系统使用的位置 -I /usr/lib
 sudo ln -s /usr/local/lib/libpacc.so.1.2.1 /usr/lib,生成链接更好用。
* 知识点 系统库路径 /usr/lib 或 /lib, ldd 用于查看执行文件所需库的状况
Action:---
Modify:---
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

LVYsince2021/LVYGrail/implStudio$ g++ -o outhello hello-world.c -I /usr/local/include/event2/ -levent -levent_core -levent_extra -levent_pthreads
hello-world.c: In function ‘void listener_cb(evconnlistener*, int, sockaddr*, int, void*)’:
hello-world.c:92:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
   92 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
hello-world.c: In function ‘void signal_cb(int, short int, void*)’:
hello-world.c:135:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
  135 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
(base) liufeng@liufeng-Lenovo:~/LVYsince2021/LVYGrail/implStudio$ gcc -o outhello hello-world.c -I /usr/local/include/event2/ -levent -levent_core -levent_extra -levent_pthreads
(base) liufeng@liufeng-Lenovo:~/LVYsince2021/LVYGrail/implStudio$ g++ -o outhello hello-world.c -I /usr/local/include/event2/ -levent -levent_core -levent_extra -levent_pthreads
hello-world.c: In function ‘void listener_cb(evconnlistener*, int, sockaddr*, int, void*)’:
hello-world.c:92:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
   92 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
hello-world.c: In function ‘void signal_cb(int, short int, void*)’:
hello-world.c:135:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
  135 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
*******************************************************************************************

gcc -o outhello hello-world.c -I /usr/local/include/event2/ -levent -levent_core -levent_extra -levent_pthreads
(base) liufeng@liufeng-Lenovo:~/LVYsince2021/LVYGrail/implStudio$ g++ -o outhello hello-world.c -I /usr/local/include/event2/ -levent -levent_core -levent_extra -levent_pthreads
hello-world.c: In function ‘void listener_cb(evconnlistener*, int, sockaddr*, int, void*)’:
hello-world.c:92:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
   92 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
hello-world.c: In function ‘void signal_cb(int, short int, void*)’:
hello-world.c:135:28: error: invalid conversion from ‘void*’ to ‘event_base*’ [-fpermissive]
  135 |  struct event_base *base = user_data;
      |                            ^~~~~~~~~
      |                            |
      |                            void*
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值