CMake TcpServer项目 链接静态库/动态库

一、链接静态库 

  • 查看项目结构
heheda@linux:~/Linux/LinuxServerCpp-Link$ tree
.
├── CMakeLists.txt
├── include
│   ├── common
│   │   ├── Buffer.h
│   │   ├── Channel.h
│   │   └── Log.h
│   ├── http
│   │   ├── HttpRequest.h
│   │   └── HttpResponse.h
│   ├── reactor
│   │   ├── Dispatcher.h
│   │   ├── EpollDispatcher.h
│   │   ├── EventLoop.h
│   │   ├── PollDispatcher.h
│   │   └── SelectDispatcher.h
│   ├── tcp
│   │   ├── TcpConnection.h
│   │   └── TcpServer.h
│   └── thread
│       ├── ThreadPool.h
│       └── WorkerThread.h
├── main.cpp
├── src
│   ├── common
│   │   ├── Buffer.cpp
│   │   └── Channel.cpp
│   ├── http
│   │   ├── HttpRequest.cpp
│   │   └── HttpResponse.cpp
│   ├── reactor
│   │   ├── Dispatcher.cpp
│   │   ├── EpollDispatcher.cpp
│   │   ├── EventLoop.cpp
│   │   ├── PollDispatcher.cpp
│   │   └── SelectDispatcher.cpp
│   ├── tcp
│   │   ├── TcpConnection.cpp
│   │   └── TcpServer.cpp
│   └── thread
│       ├── ThreadPool.cpp
│       └── WorkerThread.cpp
└── staticLib
    ├── libcommon.a
    ├── libhttp.a
    ├── libreactor.a
    ├── libtcp.a
    └── libthread.a

13 directories, 34 files
heheda@linux:~/Linux/LinuxServerCpp-Link$ 

CMake TcpServer项目 链接静态库/动态库_第1张图片

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HttpWeb)

# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/staticLib)

#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
set(INCLUDE ${PROJECT_SOURCE_DIR}/include)
include_directories(${INCLUDE}/common)
include_directories(${INCLUDE}/http)
include_directories(${INCLUDE}/reactor)
include_directories(${INCLUDE}/tcp)
include_directories(${INCLUDE}/thread)

file(GLOB SRC_LIST ${CMAKE_CURRENT_LIST_DIR}/src/*/*.cpp)
# 包含静态库路径
link_directories(${LIBPATH})

# 链接静态库, calc为静态库的名字
link_libraries(common http reactor tcp thread -lpthread)
add_executable(server main.cpp ${SRC_LIST})
# target_link_libraries(server common http reactor tcp thread -lpthread)

# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量

  •  执行命令和结果:
heheda@linux:~/Linux/LinuxServerCpp-Link$ mkdir build
heheda@linux:~/Linux/LinuxServerCpp-Link$ cd build/
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- 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
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/LinuxServerCpp-Link/build
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ make
Scanning dependencies of target server
[  6%] Building CXX object CMakeFiles/server.dir/main.cpp.o
[ 13%] Building CXX object CMakeFiles/server.dir/src/common/Buffer.cpp.o
[ 20%] Building CXX object CMakeFiles/server.dir/src/common/Channel.cpp.o
[ 26%] Building CXX object CMakeFiles/server.dir/src/http/HttpRequest.cpp.o
[ 33%] Building CXX object CMakeFiles/server.dir/src/http/HttpResponse.cpp.o
[ 40%] Building CXX object CMakeFiles/server.dir/src/reactor/Dispatcher.cpp.o
[ 46%] Building CXX object CMakeFiles/server.dir/src/reactor/EpollDispatcher.cpp.o
[ 53%] Building CXX object CMakeFiles/server.dir/src/reactor/EventLoop.cpp.o
[ 60%] Building CXX object CMakeFiles/server.dir/src/reactor/PollDispatcher.cpp.o
[ 66%] Building CXX object CMakeFiles/server.dir/src/reactor/SelectDispatcher.cpp.o
[ 73%] Building CXX object CMakeFiles/server.dir/src/tcp/TcpConnection.cpp.o
[ 80%] Building CXX object CMakeFiles/server.dir/src/tcp/TcpServer.cpp.o
[ 86%] Building CXX object CMakeFiles/server.dir/src/thread/ThreadPool.cpp.o
[ 93%] Building CXX object CMakeFiles/server.dir/src/thread/WorkerThread.cpp.o
[100%] Linking CXX executable ../bin/server
[100%] Built target server
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ cd ../bin
heheda@linux:~/Linux/LinuxServerCpp-Link/bin$ ./server
DEBUG: /home/heheda/Linux/LinuxServerCpp-Link/src/tcp/TcpServer.cpp@run, line: 66
***LogInfo[服务器程序已经启动了...]

 CMake TcpServer项目 链接静态库/动态库_第2张图片

二、链接动态库

  • 查看项目结构
heheda@linux:~/Linux/LinuxServerCpp-Link$ tree
.
├── CMakeLists.txt
├── include
│   ├── common
│   │   ├── Buffer.h
│   │   ├── Channel.h
│   │   └── Log.h
│   ├── http
│   │   ├── HttpRequest.h
│   │   └── HttpResponse.h
│   ├── reactor
│   │   ├── Dispatcher.h
│   │   ├── EpollDispatcher.h
│   │   ├── EventLoop.h
│   │   ├── PollDispatcher.h
│   │   └── SelectDispatcher.h
│   ├── tcp
│   │   ├── TcpConnection.h
│   │   └── TcpServer.h
│   └── thread
│       ├── ThreadPool.h
│       └── WorkerThread.h
├── main.cpp
└── sharedLib
    ├── libcommon.so
    ├── libhttp.so
    ├── libreactor.so
    ├── libtcp.so
    └── libthread.so

7 directories, 21 files
heheda@linux:~/Linux/LinuxServerCpp-Link$ 

CMake TcpServer项目 链接静态库/动态库_第3张图片

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(HttpWeb)

# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/sharedLib)

#包含头文件 注意:PROJECT_SOURCE_DIR 或 CMAKE_CURRENT_SOURCE_DIR
set(INCLUDE ${PROJECT_SOURCE_DIR}/include)
include_directories(${INCLUDE}/common)
include_directories(${INCLUDE}/http)
include_directories(${INCLUDE}/reactor)
include_directories(${INCLUDE}/tcp)
include_directories(${INCLUDE}/thread)

# link_libraries(common http reactor tcp thread -lpthread)
# link_directories(${LIB_PATH})
# file(GLOB SRC_LIST ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
# 包含静态库路径
# link_directories(${PROJECT_SOURCE_DIR}/lib)
link_directories(${LIBPATH})

# 链接静态库, calc为静态库的名字
# link_libraries(server common http reactor tcp thread -lpthread)
# add_executable(server main.cpp ${SRC_LIST})
add_executable(server main.cpp)
target_link_libraries(server common http reactor tcp thread -lpthread)

# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量

  • 执行命令和结果:
heheda@linux:~/Linux/LinuxServerCpp-Link$ mkdir build
heheda@linux:~/Linux/LinuxServerCpp-Link$ cd build/
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- 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
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/LinuxServerCpp-Link/build
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ make
Scanning dependencies of target server
[ 50%] Building CXX object CMakeFiles/server.dir/main.cpp.o
[100%] Linking CXX executable ../bin/server
[100%] Built target server
heheda@linux:~/Linux/LinuxServerCpp-Link/build$ cd ../bin
heheda@linux:~/Linux/LinuxServerCpp-Link/bin$ ls
server
heheda@linux:~/Linux/LinuxServerCpp-Link/bin$ ./server
DEBUG: /home/heheda/Linux/LinuxServerCppInclude/tcp/src/TcpServer.cpp@run, line: 66
***LogInfo[服务器程序已经启动了...]

CMake TcpServer项目 链接静态库/动态库_第4张图片

你可能感兴趣的:(CMake,笔记,c++)