optim icon indicating copy to clipboard operation
optim copied to clipboard

How to build OptimLib into shared library with MSVC compiler under Windows OS?

Open ruixunbao opened this issue 5 years ago • 2 comments

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)?

ruixunbao avatar May 30 '19 14:05 ruixunbao

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}
)


Davar1990 avatar Nov 17 '20 00:11 Davar1990

It would be great if this library can be used via package managers like vcpkg.

tueda avatar Nov 30 '20 11:11 tueda