Steven G. Johnson <stevenj....@...> writes:

> 
> 
> 
> On Jul 12, 2010, at 12:30 PM, Wisam Yacteen wrote:
> I'm having a little trouble getting the NLOpt source code to compile using 
Visual Studio on Windows.  I have project using the nlopt dll working fine but 
I 
would like to be able to step into the source while debugging.  Does anyone 
already have a visual studio project that contains all the nlopt source that 
you 
wouldn't mind sharing with me?
> 
> 
> I don't know of any one who has done this offhand (please shout out if you 
have).
> 
> I'm guessing that the most annoying part of compiling under VC++ may be hand-
creating the config.h file, which on Unix-like systems is generated 
automatically from the configure script. 
 I've put the config.h file generated during building of the 32-
bit Windows DLL with MinGW at http://ab-initio.mit.edu/nlopt/config.h in case 
it 
is helpful.  Try using it unmodified, and only change if you get a compiler 
error that results from one of the #define's in this file.
> 
> Steven
> 


I just discovered NLOPT yesterday and i loved it. I also wanted to use it
with VS, and i generally used Cmake.
So today i just wrote the files: CMakeList.txt / config.cmake.h.in
(see after)

It allowed me to compile with almost no code modification 
(you can just put these files in nlopt-2.2/ )

The only thing i changed was:

- rename #include "config.h" to #include "config_nlopt.h"
 because my project already had a config.h (bad idea)

- in optimize.c: (line 375)
         case NLOPT_GD_MLSL_LDS: {
              if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
              nlopt_opt local_opt = opt->local_opt;
              nlopt_result ret;       

to

         case NLOPT_GD_MLSL_LDS: {
              nlopt_opt local_opt = opt->local_opt;
              nlopt_result ret;
              if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
             
 because VC was in 'C' mode, and not 'C++', and you can define a 
 variable only at the beginning of a block in C
 (I tried for one hour to force VC to use C++ but without any success)


- in options.c (line 129)
 (in nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt)  )
also put 
          nlopt_munge munge;
at the beginning of the function and then after
          munge = nopt->munge_on_copy;



And success!
Maybe Steven you could include these modifs in the original source code?
(and if you want to CMakeList.txt ?)

Thanks


//////////////////////////////////////////////////////////////
CMakeList.txt  
NOTE: the CMakeList.txt only declare the nlopt C/C++ library
do not consider fortran, python, etc...
//////////////////////////////////////////////////////////////

#==============================================================================
# NLOPT CMake file
# 
# NLopt is a free/open-source library for nonlinear optimization, providing 
# a common interface for a number of different free optimization routines 
# available online as well as original implementations of various other 
# algorithms
# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt 
# AUTHOR: Steven G. Johnson
#
# This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility.
# Benoit Scherrer, 2010 CRL, Harvard Medical School
# Copyright (c) 2008-2009 Children's Hospital Boston 
# Minor changes to the source was applied to make possible the compilation with
# Cmake under Linux/Win32
#==============================================================================


CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckTypeSize)


#==============================================================================
# COMPILATION CHECKINGS and CONFIGURATION GENERATION
#==============================================================================
CHECK_INCLUDE_FILES( unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILES( string.h HAVE_STRING_H)
CHECK_INCLUDE_FILES( strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILES( sys/stat.h HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILES( inttypes.h HAVE_INTTYPES_H)
CHECK_INCLUDE_FILES( memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILES( stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILES( stdint.h HAVE_STDINT_H)

CHECK_INCLUDE_FILES( sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES( sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES( sys/types.h HAVE_SYS_TYPES_H)

CHECK_FUNCTION_EXISTS( isinf HAVE_ISINF)
CHECK_FUNCTION_EXISTS( isinf HAVE_ISNAN)
CHECK_FUNCTION_EXISTS( gettimeofday HAVE_GETTIMEOFDAY)
CHECK_FUNCTION_EXISTS( qsort_r HAVE_QSORT_R)
CHECK_FUNCTION_EXISTS( time HAVE_TIME)

CHECK_TYPE_SIZE(uint32_t    UINT32_T)
IF(NOT HAVE_UINT32_T)
  IF(MSVC)
    SET(uint32_t "unsigned int")
    SET(SIZEOF_UNSIGNED_INT 4)
  ENDIF(MSVC)
ENDIF(NOT HAVE_UINT32_T)

SET( NLOPT_MAJOR_VERSION 2 )
SET( NLOPT_MINOR_VERSION 2 )

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.h.in 
${PROJECT_BINARY_DIR}/config_nlopt.h IMMEDIATE )


#==============================================================================
# INCLUDE DIRECTORIES
#==============================================================================
INCLUDE_DIRECTORIES (
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${PROJECT_BINARY_DIR}
        stogo 
        util 
        direct 
        cdirect 
        praxis 
        luksan 
        crs 
        mlsl 
        mma 
        cobyla 
        newuoa 
        neldermead 
        auglag 
        bobyqa 
        isres 
        slsqp
        api   )


#==============================================================================
# nlopt LIBRARY TARGET
#==============================================================================
ADD_LIBRARY ( nlopt   
        direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c 
direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
        cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
        praxis/praxis.c praxis/praxis.h
        luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c 
luksan/pssubs.c luksan/luksan.h
        crs/crs.c crs/crs.h
        mlsl/mlsl.c mlsl/mlsl.h
        mma/mma.c mma/mma.h
        cobyla/cobyla.c cobyla/cobyla.h
        newuoa/newuoa.c newuoa/newuoa.h 
        neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c   
        auglag/auglag.c auglag/auglag.h
        bobyqa/bobyqa.c bobyqa/bobyqa.h
        isres/isres.c isres/isres.h 
        slsqp/slsqp.c slsqp/slsqp.h 
        api/general.c api/options.c api/optimize.c api/deprecated.c api/nlopt-
internal.h api/nlopt.h api/f77api.c api/f77funcs.h api/f77funcs_.h 
api/nlopt.hpp 
api/nlopt-in.hpp
        util/mt19937ar.c util/sobolseq.c util/soboldata.h util/timer.c 
util/stop.c util/nlopt-util.h util/redblack.c util/redblack.h util/qsort_r.c 
util/rescale.c 
        stogo/global.cc stogo/linalg.cc stogo/local.cc stogo/stogo.cc 
stogo/tools.cc stogo/global.h stogo/linalg.h stogo/local.h stogo/stogo_config.h 
stogo/stogo.h stogo/tools.h 
        )

////////////////////////////////////////////////////////////////
 second file: config.cmake.h.in
cf the #define copysign(x, y) ( ((x < 0 && y > 0) || (x > 0 && y < 0)) ? -x : x 
)

at the end for WIN32
///////////////////////////////////////////////////////////////
/*==============================================================================
# NLOPT CMake configuration file
# 
# NLopt is a free/open-source library for nonlinear optimization, providing 
# a common interface for a number of different free optimization routines 
# available online as well as original implementations of various other 
# algorithms
# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt 
# AUTHOR: Steven G. Johnson
#
# This config.cmake.h.in file was created to compile NLOPT with the CMAKE 
utility.
# Benoit Scherrer, 2010 CRL, Harvard Medical School
# Copyright (c) 2008-2009 Children's Hospital Boston 
#
# Minor changes to the source was applied to make possible the compilation with
# Cmake under Linux/Win32
#============================================================================*/

/* Bugfix version number. */
#define BUGFIX_VERSION 0

/* Define to enable extra debugging code. */
#undef DEBUG

/* Define to 1 if you have the `BSDgettimeofday' function. */
#undef HAVE_BSDGETTIMEOFDAY

/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H

/* Define to 1 if you have the <getopt.h> header file. */
#undef HAVE_GETOPT_H

/* Define to 1 if you have the `getpid' function. */
#undef HAVE_GETPID

/* Define if syscall(SYS_gettid) available. */
#undef HAVE_GETTID_SYSCALL

/* Define to 1 if you have the `gettimeofday' function. */
#cmakedefine HAVE_GETTIMEOFDAY

/* Define to 1 if you have the <inttypes.h> header file. */
#cmakedefine HAVE_INTTYPES_H

/* Define if the isinf() function/macro is available. */
#cmakedefine HAVE_ISINF

/* Define if the isnan() function/macro is available. */
#cmakedefine HAVE_ISNAN

/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM

/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H

/* Define to 1 if you have the `qsort_r' function. */
#cmakedefine HAVE_QSORT_R

/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H

/* Define to 1 if you have the <stdlib.h> header file. */
#cmakedefine HAVE_STDLIB_H

/* Define to 1 if you have the <strings.h> header file. */
#cmakedefine HAVE_STRINGS_H

/* Define to 1 if you have the <string.h> header file. */
#cmakedefine HAVE_STRING_H

/* Define to 1 if you have the <sys/stat.h> header file. */
#cmakedefine HAVE_SYS_STAT_H

/* Define to 1 if you have the <sys/types.h> header file. */
#cmakedefine HAVE_SYS_TYPES_H

/* Define to 1 if you have the `time' function. */
#cmakedefine HAVE_TIME

/* Define to 1 if the system has the type `uint32_t'. */
#cmakedefine HAVE_UINT32_T

/* Define to 1 if you have the <unistd.h> header file. */
#cmakedefine HAVE_UNISTD_H

/* Define to the sub-directory in which libtool stores uninstalled libraries.
   */
#undef LT_OBJDIR

/* Major version number. */
#define MAJOR_VERSION @NLOPT_MAJOR_VERSION@

/* Minor version number. */
#define MINOR_VERSION @NLOPT_MINOR_VERSION@

/* Name of package */
#undef PACKAGE

/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT

/* Define to the full name of this package. */
#undef PACKAGE_NAME

/* Define to the full name and version of this package. */
#undef PACKAGE_STRING

/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME

/* Define to the home page for this package. */
#undef PACKAGE_URL

/* Define to the version of this package. */
#undef PACKAGE_VERSION

/* replacement for broken HUGE_VAL macro, if needed */
#undef REPLACEMENT_HUGE_VAL

/* The size of `unsigned int', as computed by sizeof. */
#define SIZEOF_UNSIGNED_INT @SIZEOF_UNSIGNED_INT@

/* The size of `unsigned long', as computed by sizeof. */
#undef SIZEOF_UNSIGNED_LONG

/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS

/* Define to C thread-local keyword, or to nothing if this is not supported in
   your compiler. */
#define THREADLOCAL

/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
#undef TIME_WITH_SYS_TIME

/* Version number of package */
#undef VERSION

/* Define if compiled including C++-based routines */
#undef WITH_CXX

/* Define if we have the non-free Nocedal LBFGS code */
#undef WITH_NOCEDAL

/* Define to empty if `const' does not conform to ANSI C. */
#undef const

/* Define to `__inline__' or `__inline' if that's what the C compiler
   calls it, or to nothing if 'inline' is not supported under any name.  */
#ifndef __cplusplus
#undef inline
#endif

#ifdef WIN32
#define copysign(x, y) ( ((x < 0 && y > 0) || (x > 0 && y < 0)) ? -x : x )
#endif






_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to