Skip to main content

using cmake -H. -Bbuild fails

Answered

Comments

9 comments

  • Christian Perau
    Curious
    Gurobi-versary
    Conversationalist

    Adding

    option(BUILD_SHARED_LIBS "Build shared libraries" ON)

    to CMakeLists.txt was missing to write a .dll, by the way. But unfortunately if I load the corresponding .dll with ctypes.cdll.LoadLibrary it cannot find the defined function.

     

    1
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Christian,

    You are not specifying the C or C++ source files in your CMakeLists.txt. The template is meant to be used with the file from our example library: mip1_c++.cpp
    And this file is not in the root directory of your project.

    You can also find that file in the examples directory of your Gurobi installation.

    To use our CMake template for your project, you need to specify all your source files and maybe also link additional libraries if needed.

    Cheers,
    Matthias

    0
  • Christian Perau
    Curious
    Gurobi-versary
    Conversationalist

    Hi Matthias,


    thanks for the quick response! I could fix the error regarding the mi1_c++.cpp file. The error regarding the variables GUROBI_CXX_DEBUG_LIBRARY and GUROBI_CXX_LIBRARY remains unfortunately. Do I have to specify those somewhere?

    -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.17763.
    -- Build type: Release
    -- The following REQUIRED packages have been found:

     * GUROBI

    -- Configuring done (0.1s)
    CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
    Please set them or make sure they are set and tested correctly in the CMake files:
    GUROBI_CXX_DEBUG_LIBRARY
        linked by target "cp_diss_model" in directory C:/Users/ws0734/PycharmProjects/cp_diss_model/C
    GUROBI_CXX_LIBRARY
        linked by target "cp_diss_model" in directory C:/Users/ws0734/PycharmProjects/cp_diss_model/C

    -- Generating done (0.0s)
    CMake Generate step failed.  Build files cannot be regenerated correctly.

    Maybe It´s best if I explain a little bit about the big picture. I am acutally mainly working in python. However, I want to try to build and solve the gurobi models in C (using ctypes from python). Basically that means constructing all arrays in pyhton (via numpy) and giving them to the C function to construct the model, solve it and return the results. That´s why I am trying to install a C project within my python project. As I see it I would not need any other libraries, nor source files (only the function I am creating in the one .c file), correct?

    Thank you very much for your help!
    Best,

    Christian

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Christian,

    I have just updated the FindGUROBI.cmake file in the article to fix a bug. Now, it should find the correct Gurobi library.

    Thanks for pointing out this issue!

    Cheers,
    Matthias

    0
  • Christian Perau
    Curious
    Gurobi-versary
    Conversationalist

    Hi Matthias,

    thanks! That fixed the problem indeed.

    After the cmake build, I tried to run the script (script.c):

    #include "gurobi_c.h"
    int main(){
    return 0;
    }

    from the terminal using gcc:

    gcc script.c

    However, I get the error "gurobi_c.h: No such file or directory". I thought by specifying the script in CMakeLists.txt before creating the build script.c would know where to find gurobi. Sorry if I understood it wrong, I am basically a beginner in C.

    The environment variable GUROBI_HOME is set to C:\gurobi1000\win64 by the way.

     

    Best,
    Christian

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Christian,

    This is not how to run binaries compiled using C. The compilation process (if successful) will result in a binary executable that you need to run. The name of this executable is specified in the CMakeLists.txt via the add_executable() function. After every change to the C source file, you need to recompile the executable using

    cmake --build build

    You cannot "run" a C file - it's not a scripting or just-in-time compilation language like Python, or Julia.

    Cheers,
    Matthias

    0
  • Christian Perau
    Curious
    Gurobi-versary
    Conversationalist

    Hi Matthias,

    thanks for the explanation! I was wondering if it was possible to create a shared object (.so) file instead of the .exe file (or additionally to it) so that the function is callable via ctypes in python?

    Best,

    Christian

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Christian,

    You can create (shared) library files in CMake using the \(\texttt{add_library()}\) command. Please note that on Windows there are no \(\texttt{.so}\) files but \(\texttt{.dll}\) files. Depending on your current operating system, CMake will chose the correct file format automatically.

    I am not sure this approach will work out in the end, and you might be better off using something like Cython: C-Extensions for Python to gain some more performance over Python.

    Cheers,
    Matthias

    0
  • Christian Perau
    Curious
    Gurobi-versary
    Conversationalist

    Hi Matthias,

    Thanks for the hint. Apparently instead of writing either a .dll or .so, the cmake build now creates a .lib file. I guess there will be some option on how to choose the file format, I´ll have a look.

    Actually it is a not so much about the performance but about memory issues. Originally I had problems with the memory usage of the Python API (https://support.gurobi.com/hc/en-us/community/posts/12387779995025-Reduce-memory-usage-of-gurobipy-Model). So I wanted to try using the C API from python by using ctypes. So basically calling a C function passing pointers to the different model attributes (lb, ub, etc.) and then build the gurobi model in C, solve it and return the results to python, so that I can avoid the dictionaries as described in the other post. That´s also why I think cython won´t help me regarding the problem because I don´t think I am able to work with the C API using cython.

    Probably not a very "beautiful" idea but I thought it´s worth a try.

    Best,
    Christian

    0

Please sign in to leave a comment.