Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ruixunbao opened this issue May 30, 2019 · 2 comments

Comments

@ruixunbao
Copy link

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

@Davar1990
Copy link

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


@tueda
Copy link

tueda commented Nov 30, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants