From d6b92a5b6536a53114cf2752fe9911f2936479ba Mon Sep 17 00:00:00 2001 From: yony Date: Thu, 30 May 2024 19:18:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=BA=93=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=80=81=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++- CMakeLists.txt | 17 ++++++++++++++--- include/Point.h | 27 ++++++++++++++++++++++++--- lib/CMakeLists.txt | 22 ++++++++++++++++++++++ {src => lib}/Point.cpp | 4 ++++ 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 lib/CMakeLists.txt rename {src => lib}/Point.cpp (94%) diff --git a/.gitignore b/.gitignore index 3fd7804..28d1c48 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ # MacOS .DS_Store # cmake build dir -build/ \ No newline at end of file +build/ +lib/Debug/ +lib/Release/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 824549f..a2b834c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.16) #------------REQUIRED_2 指定项目名及项目版本------------# project( - WireRouting # 项目名称没有什么特别的用处。这里没有添加任何的目标(target) + WireRoutingProject # 项目名称没有什么特别的用处。这里没有添加任何的目标(target) VERSION 1.0 DESCRIPTION "A project for wire routing" # CMake 3.9+ 特性 # 语言可以是 C,CXX,Fortran,ASM,CUDA(CMake 3.8+),CSharp(3.8+),SWIFT(CMake 3.15+ experimental) @@ -64,7 +64,7 @@ message(STATUS "Self added CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}") # igl_include(glfw) # igl_include_optional(glfw) Enable the target igl::glfw - +ADD_SUBDIRECTORY(lib) #------------REQUIRED_3 添加可执行文件------------# # 不要使用file函数中GLOB来收集文件:如果不重新运行 CMake,Make 或者其他的工具将不会知道你是否添加了某个文件 @@ -75,6 +75,12 @@ add_executable( ${SRC_DIR} ) +# add_custom_command( +# TARGET ${PROJECT_NAME} POST_BUILD +# COMMAND ${CMAKE_COMMAND} -E copy_directory +# ${CMAKE_SOURCE_DIR}/lib/$(IntDir)/WireRouting.dll $(outdir) +# ) +file(COPY ${CMAKE_SOURCE_DIR}/lib/Debug/WireRouting.dll DESTINATION ${PROJECT_BINARY_DIR}/Debug) #------------REQUIRED_5.2 将库链接到目标(通常是一个可执行文件),这个命令告诉 CMake 在构建目标时需要链接哪些库------------# # target_link_libraries(${PROJECT_NAME} PUBLIC {lib_name1} {lib_name1} ...) @@ -93,6 +99,11 @@ else() ) endif() +target_link_libraries( + ${PROJECT_NAME} PUBLIC + ${PROJECT_SOURCE_DIR}/lib/Debug/WireRouting.lib +) + #------------OPTIONAL_1.2 添加生成的config.h头文件所在目录------------# # 为目标添加了一个目录,源码中的 #include "config.h" 将会被解析为 ${PROJECT_BINARY_DIR}/config.h @@ -102,7 +113,7 @@ target_include_directories( # INTERFACE(只影响依赖) ${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR} # config.h.in 会生成配置在 build 目录 - include + ${CMAKE_SOURCE_DIR}/include ${TINYXML_INCLUDE_DIR} # tinyxml库的头文件目录 ${TINYXML_INCLUDE_DIRS} # pkg-config tinyxml dir ) diff --git a/include/Point.h b/include/Point.h index fc540af..424b2df 100644 --- a/include/Point.h +++ b/include/Point.h @@ -1,11 +1,32 @@ +#pragma once + +#ifdef MY_LIB_SHARED_BUILD + #ifdef _WIN32 + #ifdef MY_LIB_EXPORTS + #define POINT_LIB_API __declspec(dllexport) + #else + #define POINT_LIB_API __declspec(dllimport) + #endif // MY_LIB_EXPORTS + #else + #define POINT_LIB_API + #endif // _WIN32 +#else + #define POINT_LIB_API +#endif // MY_LIB_SHARED_BUILD + + #include #include #include #include + using namespace std; -const double eps = 1e-6; -#define Equal(a, b) ((fabs(a - b) < eps) ? true : false) -struct P + +extern "C" POINT_LIB_API const double eps = 1e-6; + +extern "C" POINT_LIB_API bool Equal(double a, double b); + +extern "C" struct POINT_LIB_API P { double x, y, z; // double dx, dy, dz; // diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..d437133 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,22 @@ +aux_source_directory(${CMAKE_SOURCE_DIR}/lib SRC_FILES_IN_LIB) +message(STATUS "SRC_FILES_IN_LIB: ${SRC_FILES_IN_LIB}") + +add_library(WireRouting SHARED ${SRC_FILES_IN_LIB}) +target_compile_definitions(WireRouting PUBLIC -DMY_LIB_SHARED_BUILD) +target_compile_definitions(WireRouting PRIVATE -DMY_LIB_EXPORTS) + +INSTALL(TARGETS WireRouting DESTINATION ${CMAKE_SOURCE_DIR}/lib) + +SET_TARGET_PROPERTIES( + WireRouting PROPERTIES LINKER_LANGUAGE C + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib + OUTPUT_NAME "WireRouting" + PREFIX "" +) + +target_include_directories( + WireRouting PUBLIC + ${CMAKE_SOURCE_DIR}/include +) diff --git a/src/Point.cpp b/lib/Point.cpp similarity index 94% rename from src/Point.cpp rename to lib/Point.cpp index 3605467..fc90072 100644 --- a/src/Point.cpp +++ b/lib/Point.cpp @@ -1,5 +1,9 @@ #include "Point.h" +bool Equal(double a, double b) +{ + return ((fabs(a - b) < eps) ? true : false); +} bool P::operator<(P B) const {