From e68e82cf5e5952e8a75342f19931a569893af2be Mon Sep 17 00:00:00 2001 From: takuto-NA Date: Wed, 4 Dec 2024 19:45:53 +0900 Subject: [PATCH 1/7] Modify CmakeLists and eigenmvn.h for Windows --- CMakeLists.txt | 12 +++++++++--- include/libcmaes/eigenmvn.h | 26 ++++++++++++++++++-------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e5a4084..d36a220c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,15 @@ if (NOT DEFINED CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type") endif () -set(CMAKE_CXX_FLAGS "-Wall -Wextra") -set(CMAKE_CXX_FLAGS_DEBUG "-g") -set(CMAKE_CXX_FLAGS_RELEASE "-O3") +if(MSVC) + set(CMAKE_CXX_FLAGS "/W4") + set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od") + set(CMAKE_CXX_FLAGS_RELEASE "/O2") +else() + set(CMAKE_CXX_FLAGS "-Wall -Wextra") + set(CMAKE_CXX_FLAGS_DEBUG "-g") + set(CMAKE_CXX_FLAGS_RELEASE "-O3") +endif() list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) diff --git a/include/libcmaes/eigenmvn.h b/include/libcmaes/eigenmvn.h index fb8b11d4..85b5f014 100644 --- a/include/libcmaes/eigenmvn.h +++ b/include/libcmaes/eigenmvn.h @@ -58,18 +58,28 @@ namespace Eigen { static std::mt19937 rng; // The uniform pseudo-random algorithm mutable std::normal_distribution norm; // gaussian combinator - EIGEN_EMPTY_STRUCT_CTOR(scalar_normal_dist_op) - - scalar_normal_dist_op &operator=(scalar_normal_dist_op &&other) - { + scalar_normal_dist_op() : norm(Scalar(0), Scalar(1)) {} + + scalar_normal_dist_op(const scalar_normal_dist_op& other) + : norm(other.norm) { + } + + scalar_normal_dist_op& operator=(const scalar_normal_dist_op& other) { if (this != &other) { - swap(other); + norm = other.norm; } return *this; } - - scalar_normal_dist_op(scalar_normal_dist_op &&other) { - *this = std::move(other); + + scalar_normal_dist_op(scalar_normal_dist_op&& other) noexcept + : norm(std::move(other.norm)) { + } + + scalar_normal_dist_op& operator=(scalar_normal_dist_op&& other) noexcept { + if (this != &other) { + norm = std::move(other.norm); + } + return *this; } template From bf635303fcadbf774239ddf42d6b27d44621bd80 Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:54:02 +0900 Subject: [PATCH 2/7] Update CMakeLists.txt * Add options to use glog and gflags * glog_DIR and gflags_DIR are required to set the directories containing CMake configuration files. --- CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d36a220c..a7bc5f9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,8 @@ option (LIBCMAES_BUILD_TESTS "build tests" OFF) option (LIBCMAES_BUILD_EXAMPLES "build examples" ${LIBCMAES_TOP_LEVEL}) option (LIBCMAES_USE_OPENMP "Use OpenMP for multithreading" ON) option (LIBCMAES_ENABLE_SURROG "support for surrogates" ON) +option (LIBCMAES_USE_GLOG "Use glog for logging" OFF) +option (LIBCMAES_USE_GFLAGS "Use gflags for command line parsing" OFF) # Offer the user the choice of overriding the installation directories set (INSTALL_LIB_DIR lib${LIB_SUFFIX} @@ -74,6 +76,14 @@ check_include_file (sys/stat.h HAVE_SYS_STAT_H) # ---------- dependencies ---------- find_package (Eigen3 3.0.0 REQUIRED) +if (LIBCMAES_USE_GLOG) + find_package (glog REQUIRED) +endif () + +if (LIBCMAES_USE_GFLAGS) + find_package (gflags REQUIRED) +endif () + if (LIBCMAES_USE_OPENMP) find_package (OpenMP QUIET) if(NOT OpenMP_CXX_FOUND) From d812d0de24d663472a9505c3a5e1c476143f6418 Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:00:42 +0900 Subject: [PATCH 3/7] Support MSVC * Add CMake options to build dynamic/static libraries * Dynamic library (DLL) with dynamically linked CRT * Static library with dynamically linked CRT * Static library with statically linked CRT --- CMakeLists.txt | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7bc5f9b..8396cddc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,15 +9,15 @@ if (NOT DEFINED CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type") endif () -if(MSVC) - set(CMAKE_CXX_FLAGS "/W4") - set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od") - set(CMAKE_CXX_FLAGS_RELEASE "/O2") -else() - set(CMAKE_CXX_FLAGS "-Wall -Wextra") - set(CMAKE_CXX_FLAGS_DEBUG "-g") - set(CMAKE_CXX_FLAGS_RELEASE "-O3") -endif() +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + set(CMAKE_CXX_FLAGS "/W4") + set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od") + set(CMAKE_CXX_FLAGS_RELEASE "/O2") +else () + set(CMAKE_CXX_FLAGS "-Wall -Wextra") + set(CMAKE_CXX_FLAGS_DEBUG "-g") + set(CMAKE_CXX_FLAGS_RELEASE "-O3") +endif () list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) @@ -35,6 +35,18 @@ option (LIBCMAES_USE_OPENMP "Use OpenMP for multithreading" ON) option (LIBCMAES_ENABLE_SURROG "support for surrogates" ON) option (LIBCMAES_USE_GLOG "Use glog for logging" OFF) option (LIBCMAES_USE_GFLAGS "Use gflags for command line parsing" OFF) +option (LIBCMAES_CRT_DYNAMIC "Use dynamic CRT for MSVC" OFF) + +# Use static/dynamic CRT for MSVC +if (LIBCMAES_CRT_DYNAMIC AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + message(STATUS "Using dynamic CRT for MSVC") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") +else () + message(STATUS "Using static CRT for MSVC") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") +endif () # Offer the user the choice of overriding the installation directories set (INSTALL_LIB_DIR lib${LIB_SUFFIX} From 200ed5bd701e5a2e16d7299f2dd2b8bb071ed36d Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:07:35 +0900 Subject: [PATCH 4/7] Remove additional modification * Removed additional modification from the original master. --- include/libcmaes/eigenmvn.h | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/include/libcmaes/eigenmvn.h b/include/libcmaes/eigenmvn.h index 85b5f014..a39beba8 100644 --- a/include/libcmaes/eigenmvn.h +++ b/include/libcmaes/eigenmvn.h @@ -57,29 +57,22 @@ namespace Eigen { public: static std::mt19937 rng; // The uniform pseudo-random algorithm mutable std::normal_distribution norm; // gaussian combinator - - scalar_normal_dist_op() : norm(Scalar(0), Scalar(1)) {} - - scalar_normal_dist_op(const scalar_normal_dist_op& other) - : norm(other.norm) { - } - - scalar_normal_dist_op& operator=(const scalar_normal_dist_op& other) { +#if defined(_MSC_VER) && _MSC_VER > 1900 + scalar_normal_dist_op() {} + scalar_normal_dist_op(const scalar_normal_dist_op&) {} +#else + EIGEN_EMPTY_STRUCT_CTOR(scalar_normal_dist_op) +#endif + scalar_normal_dist_op &operator=(scalar_normal_dist_op &&other) + { if (this != &other) { - norm = other.norm; + swap(other); } return *this; } - - scalar_normal_dist_op(scalar_normal_dist_op&& other) noexcept - : norm(std::move(other.norm)) { - } - - scalar_normal_dist_op& operator=(scalar_normal_dist_op&& other) noexcept { - if (this != &other) { - norm = std::move(other.norm); - } - return *this; + + scalar_normal_dist_op(scalar_normal_dist_op &&other) { + *this = std::move(other); } template From cb4fc5bbb25f12e493f8fb3b4c2b09e59ce908ed Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:15:02 +0900 Subject: [PATCH 5/7] Modify contour.h * Changed `contour` class to template class * To avoid the following error when compiling with MSVC : ``` 3>cmaes.lib(errstats.obj) : error LNK2005: "class std::basic_ostream > & __cdecl libcmaes::operator<<(class std::basic_ostream > &,class libcmaes::contour const &)" (??6libcmaes@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBVcontour@0@@Z) already defined in test-functions.obj 3>.\build\tests\Release\test-functions.exe : fatal error LNK1169: one or more multiply defined symbols found 3>Done building project "test-functions.vcxproj" -- FAILED. ``` * `contour` is now `contour_<0>`. No need to change the existing code. --- include/libcmaes/contour.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/libcmaes/contour.h b/include/libcmaes/contour.h index 19f47533..8cbd987a 100644 --- a/include/libcmaes/contour.h +++ b/include/libcmaes/contour.h @@ -47,11 +47,12 @@ namespace libcmaes /** * \brief function contour as a set of points and values. */ - class contour + template + class contour_ { public: - contour() {} - ~contour() {} + contour_() {} + ~contour_() {} /** * \brief add a contour point. @@ -85,7 +86,10 @@ namespace libcmaes std::vector> _points; }; - std::ostream& operator<<(std::ostream &out, const contour &c) + typedef contour_<0> contour; + + template + std::ostream& operator<<(std::ostream &out, const contour_ &c) { c.print(out); return out; From 358012e34f593929d26427029803dff700fcfdd4 Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:18:34 +0900 Subject: [PATCH 6/7] Modify test-functions.cc * `M_PI` etc. are not defined by default in MSVC. * The `_USE_MATH_DEFINES` macro is required to define them. --- tests/test-functions.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test-functions.cc b/tests/test-functions.cc index d4144c48..f34595c8 100644 --- a/tests/test-functions.cc +++ b/tests/test-functions.cc @@ -19,6 +19,9 @@ * along with libcmaes. If not, see . */ +#if defined(_MSC_VER) +#define _USE_MATH_DEFINES +#endif #include "libcmaes/cmaes.h" #include "libcmaes/errstats.h" #include From 657314c4e22df4d10bb8b277adcbdc29df85a81a Mon Sep 17 00:00:00 2001 From: tsadakane <40597344+tsadakane@users.noreply.github.com> Date: Fri, 10 Jan 2025 13:27:16 +0900 Subject: [PATCH 7/7] Fix: LIBCMAES_USE_GLOG option in CMake not working --- CMakeLists.txt | 33 +++++++++++++++++++++++++++++---- examples/CMakeLists.txt | 6 ++++++ src/CMakeLists.txt | 3 +++ tests/CMakeLists.txt | 6 ++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8396cddc..ca95f61d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,12 +88,37 @@ check_include_file (sys/stat.h HAVE_SYS_STAT_H) # ---------- dependencies ---------- find_package (Eigen3 3.0.0 REQUIRED) -if (LIBCMAES_USE_GLOG) - find_package (glog REQUIRED) +if (LIBCMAES_USE_GFLAGS) + find_package (gflags CONFIG REQUIRED) + message(STATUS "GFLAGS_INCLUDE_DIR: ${GFLAGS_INCLUDE_DIR}") + include_directories(${GFLAGS_INCLUDE_DIR}) + + if(CMAKE_BUILD_TYPE STREQUAL "Release") + get_target_property(GFLAGS_LOCATION_RELEASE gflags LOCATION_RELEASE) + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LOCATION_RELEASE} DIRECTORY) + else() + get_target_property(GFLAGS_LOCATION_DEBUG gflags LOCATION_DEBUG) + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LOCATION_DEBUG} DIRECTORY) + endif() + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LIB_DIR} DIRECTORY) + set (GFLAGS_LIB_DIR ${GFLAGS_LIB_DIR}/lib) + message(STATUS "GFLAGS_LIB_DIR: ${GFLAGS_LIB_DIR}") + link_directories(${GFLAGS_LIB_DIR}) +else() + if (LIBCMAES_BUILD_TESTS OR LIBCMAES_BUILD_EXAMPLES) + message(FATAL_ERROR "LIBCMAES_BUILD_TESTS or LIBCMAES_BUILD_EXAMPLES requires LIBCMAES_USE_GFLAGS") + endif() endif () -if (LIBCMAES_USE_GFLAGS) - find_package (gflags REQUIRED) +if (LIBCMAES_USE_GLOG) + if (NOT LIBCMAES_USE_GFLAGS) + message(FATAL_ERROR "LIBCMAES_USE_GLOG requires LIBCMAES_USE_GFLAGS") + endif () + message(STATUS "Using glog for logging") + find_package(glog CONFIG REQUIRED) + add_definitions( -DGLOG_USE_GLOG_EXPORT) + add_definitions (-DHAVE_GLOG) + add_definitions (-DHAVE_LIB_GLOG) endif () if (LIBCMAES_USE_OPENMP) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d5696cc2..e4cbdeb3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,12 @@ macro (cmaes_add_example name) add_executable (sample-${name} sample-${name}.cc) target_link_libraries (sample-${name} cmaes) + if (LIBCMAES_USE_GLOG) + target_link_libraries (sample-${name} glog gflags) + if (WIN32) + target_link_libraries (sample-${name} Dbghelp winmm shlwapi) + endif() + endif () endmacro () cmaes_add_example (code) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 397bb461..1c196cb0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,6 +60,9 @@ target_link_libraries (cmaes PUBLIC Eigen3::Eigen) if (LIBCMAES_USE_OPENMP) target_link_libraries (cmaes PUBLIC OpenMP::OpenMP_CXX) endif () +if (LIBCMAES_USE_GLOG) + target_link_libraries (cmaes PUBLIC glog) +endif () target_compile_features (cmaes PUBLIC cxx_nonstatic_member_init) if (${CMAKE_VERSION} VERSION_GREATER 3.8) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cf8620b6..e403743c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,12 @@ macro (cmaes_add_test name) add_executable (${name} ${name}.cc) target_link_libraries (${name} cmaes gflags) + if (LIBCMAES_USE_GLOG) + target_link_libraries (${name} glog) + if (WIN32) + target_link_libraries (${name} Dbghelp winmm shlwapi) + endif() + endif () add_test (NAME ${name} COMMAND t_${name}) if (WIN32) set_tests_properties (