0
回答
cmake 怎样安装自己写的动态库
【腾讯云】学生服务器套餐10元/月 >>>   

目录结构:


[root@laolang c]# tree
.
├── CMakeLists.txt
├── COPYRIGHT
├── doc
│   └── hello.txt
├── include
│   └── hello.h
├── README
└── src
    ├── CMakeLists.txt
    ├── hello
    │   ├── CMakeLists.txt
    │   └── hello.c
    └── main
        ├── CMakeLists.txt
        └── main.c

5 directories, 10 files
[root@laolang c]#



顶层CMakeLists.txt



# 指定 cmake 版本
cmake_minimum_required(VERSION 3.3)

# 输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


# 工程名与语言类型
PROJECT (hello C)


# 安装相关

# 普通文件安装
INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/t2)

# 脚本文件安装
#INSTALL(PROGRAMS runhello.sh DESTINATION bin)

# 安装目录
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/t2)

# 添加子目录
ADD_SUBDIRECTORY(src)




src/CMakeLists.txt


ADD_SUBDIRECTORY(main)
ADD_SUBDIRECTORY(hello)




src/main/CMakeLists.txt


# 源文件集合
AUX_SOURCE_DIRECTORY(. srcs)

# 头文件目录
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include )

# 目标文件
ADD_EXECUTABLE(sayhello ${srcs})

# 需要连接的库
TARGET_LINK_LIBRARIES(sayhello
    hello
    )

# 安装
INSTALL(TARGETS sayhello RUNTIME DESTINATION bin)




src/hello/CMakeLists.txt


AUX_SOURCE_DIRECTORY(. srcs)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include )

# 动态库
ADD_LIBRARY(hello SHARED ${srcs})

# 静态库
ADD_LIBRARY(hello_static STATIC ${srcs})

# 指定静态库的输出名称
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")

# 动态库与静态库同时存在
SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# 指定动态库版本
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)

# 安装
# 库文件
#INSTALL(TARGETS hello hello_static
#    LIBRARY DESTINATION lib
#    ARCHIVE DESTINATION lib
#    )
INSTALL(TARGETS hello hello_static
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib)
# 头文件
INSTALL(FILES ${PROJECT_SOURCE_DIR}/include/hello.h DESTINATION include)




源码

include/hello.h


#ifndef HELLO_H_INCLUDE
#define HELLO_H

void sayHello();

#endif




src/hello/hello.c


#include"../../include/hello.h"
#include<stdio.h>

void sayHello(){
    printf("\n\nHello World!\n");
}



src/main/main.c



#include<stdio.h>
#include"../../include/hello.h"
int main(void){
    sayHello();
    return 0;
}




执行过程


[root@laolang c]# mkdir build
[root@laolang c]# cd build/
[root@laolang build]# cmake .. -DCMAKE_INSTALL_PREFIX=/opt/mysoft/hello
-- The C compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/code/cmake/hello/c/build
[root@laolang build]# make
Scanning dependencies of target hello
[ 16%] Building C object src/hello/CMakeFiles/hello.dir/hello.c.o
[ 33%] Linking C shared library ../../lib/libhello.so
[ 33%] Built target hello
Scanning dependencies of target sayhello
[ 50%] Building C object src/main/CMakeFiles/sayhello.dir/main.c.o
[ 66%] Linking C executable ../../bin/sayhello
[ 66%] Built target sayhello
Scanning dependencies of target hello_static
[ 83%] Building C object src/hello/CMakeFiles/hello_static.dir/hello.c.o
[100%] Linking C static library ../../lib/libhello.a
[100%] Built target hello_static
[root@laolang build]# make install
[ 33%] Built target hello
[ 66%] Built target sayhello
[100%] Built target hello_static
Install the project...
-- Install configuration: ""
-- Installing: /opt/mysoft/hello/share/doc/cmake/t2/COPYRIGHT
-- Installing: /opt/mysoft/hello/share/doc/cmake/t2/README
-- Up-to-date: /opt/mysoft/hello/share/doc/cmake/t2
-- Installing: /opt/mysoft/hello/share/doc/cmake/t2/hello.txt
-- Installing: /opt/mysoft/hello/bin/sayhello
-- Removed runtime path from "/opt/mysoft/hello/bin/sayhello"
-- Installing: /opt/mysoft/hello/lib/libhello.so.1.0
-- Installing: /opt/mysoft/hello/lib/libhello.so.1
-- Installing: /opt/mysoft/hello/lib/libhello.so
-- Installing: /opt/mysoft/hello/lib/libhello.a
-- Installing: /opt/mysoft/hello/include/hello.h
[root@laolang build]#




安装目录


[root@laolang mysoft]# tree
.
└── hello
    ├── bin
    │   └── sayhello
    ├── include
    │   └── hello.h
    ├── lib
    │   ├── libhello.a
    │   ├── libhello.so -> libhello.so.1
    │   ├── libhello.so.1 -> libhello.so.1.0
    │   └── libhello.so.1.0
    └── share
        └── doc
            └── cmake
                └── t2
                    ├── COPYRIGHT
                    ├── hello.txt
                    └── README

8 directories, 9 files
[root@laolang mysoft]#




问题

执行报错:



[root@laolang bin]# ./sayhello 
./sayhello: error while loading shared libraries: libhello.so.1: cannot open shared object file: No such file or directory
[root@laolang bin]#



ldd 查看



[root@laolang bin]# ldd sayhello 
	linux-vdso.so.1 =>  (0x00007ffff91ff000)
	libhello.so.1 => not found
	libc.so.6 => /lib64/libc.so.6 (0x000000340b600000)
	/lib64/ld-linux-x86-64.so.2 (0x000000340ae00000)
[root@laolang bin]#




但是我在 build/bin 目录下可以执行,用 ldd 命令查看 也没问题


如果使用默认安装路径的提示也是这样。使用了ldconfig也不管用

请问应该怎样写CMakeLists.txt脚本才不会出现这种找不到库文件的错误


举报
小代码2016
发帖于2年前 0回/454阅
顶部