optim
optim copied to clipboard
How to build OptimLib into shared library with MSVC compiler under Windows OS?
This is a fantastic C++ optimization library. I have compiled the examples with MSVC compiler and they ran well under Windows OS. I wonder if there is a guide to build OptimLib into windows dynamic link library (dll)?
I created the following CMakeList.txt
You can probably use that as well. Note that I'm using this library with armadillo. So I had previously built openblas
and lapack
. You can modify the CMakelist.txt
to use Eigen
instead.
cmake_minimum_required(VERSION 2.8.3)
project(optim)
#================================ set proper directories ============================
#set the module directory
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
#build the executable in the binary directory on MS Visual Studio
if ( MSVC )
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
endif ( MSVC )
#================================ Compiler ========================================
# Turn on C++11
add_definitions(-std=c++11)
add_definitions(-DOPTIM_ENABLE_ARMA_WRAPPERS)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
#================================ external packages ========================================
FIND_PACKAGE(Threads REQUIRED)
FIND_PACKAGE(X11)
option(OPTIM_HEADER_ONLY OFF)
#================================ include directories ========================================
set(ARMADILLO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/3rdparty/armadillo/include)
if(OPTIM_HEADER_ONLY)
set(OPTIM_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/header_only_version)
else()
set(OPTIM_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include)
endif()
include_directories(
${OPTIM_INCLUDE_DIRS}
${ARMADILLO_INCLUDE_DIRS}
)
#================================ executables ========================================
file(GLOB SRC
"${CMAKE_SOURCE_DIR}/src/constrained/*.cpp"
"${CMAKE_SOURCE_DIR}/src/line_search/*.cpp"
"${CMAKE_SOURCE_DIR}/src/unconstrained/*.cpp"
"${CMAKE_SOURCE_DIR}/src/zeros/*.cpp"
)
add_library(optim SHARED
${SRC}
)
# add_executable(optim
# ${CMAKE_SOURCE_DIR}/tests/unconstrained/bfgs.cpp
# )
#================================ linking ========================================
if(WIN32)
set(OPENBLAS_LIBRARIES "${CMAKE_SOURCE_DIR}/lib/openblas.lib") #fast replacement for BLAS
set(LAPACK_LIBRARIES "${CMAKE_SOURCE_DIR}/lib/liblapack.lib")
# if(NOT OPTIM_HEADER_ONLY)
# set(OPTIM_LIBRARIES "${CMAKE_SOURCE_DIR}/lib/optim.lib")
# endif()
endif(WIN32)
target_link_libraries(optim
${X11_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${OPENBLAS_LIBRARIES}
${LAPACK_LIBRARIES}
# ${OPTIM_LIBRARIES}
)
It would be great if this library can be used via package managers like vcpkg.