1. 下载与安装
1.1 下载Gurobi源码
下载压缩包https://packages.gurobi.com/9.5/gurobi9.5.0_linux64.tar.gz
,直接复制压缩包到浏览器就可以下载。或者去官网下载。点击那个64-bit Linux
下载。
下载完后解压并复制文件到/opt/
目录下
tar xvfz gurobi9.5.0_linux64.tar.gz
sudo cp -r gurobi9.5.0_linux64/gurobi950/ /opt/
1.2 添加license
cd /opt/gurobi950/linux64/bin/
grbgetkey f5a6a6f0-xxxx-xxxx-xxxx-xxxxxxxxxx
备注
1)grbgetkey后面那一串为在Gurobi官网申请的个人序列号
2)可能会显示没有权限,那可以运行chmod +x grbgetkey
1.2 配置路径
vim ~/.bashrc
添加下面信息到.bashrc
export GUROBI_HOME="/opt/gurobi950/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"
export GRB_LICENSE_FILE="/home/xxx/gurobi.lic"
备注
1)最后一行为上一步生成license的路径,替换成自己的
1.3 生成libgurobi_c++.a
cd /opt/gurobi950/linux64/src/build/
sudo make
sudo cp libgurobi_c++.a ../../lib/
2. 配置CMakeLists
2.1 添加.cmake文件
在CMakeLists所在目录下添加文件FindGUROBI.cmake
# ------------------------------------------------------------------------------
# This file sets up Gurobi for CMake. Once done this will define
#
# GUROBI_FOUND - system has GUROBI
# GUROBI_INCLUDE_DIRS - the GUROBI include directories
# GUROBI_LIBRARIES - Link these to use GUROBI
#
# In your CMakeLists file, you need to add, e.g. (modify it if necessary):
# if (GUROBI_FOUND)
# message(STATUS "Gurobi include dir: " ${GUROBI_INCLUDE_DIRS})
# message(STATUS "Gurobi libraries: " ${GUROBI_LIBRARIES})
# target_compile_definitions(${PROJECT_NAME} PUBLIC HAS_GUROBI)
# target_include_directories(${PROJECT_NAME} PRIVATE ${GUROBI_INCLUDE_DIRS})
# target_link_libraries(${PROJECT_NAME} PRIVATE ${GUROBI_LIBRARIES})
# endif()
# ------------------------------------------------------------------------------
# Is it already configured?
if (NOT GUROBI_FOUND)
# Hardcoded search paths
set(SEARCH_PATHS_FOR_HEADERS
"$ENV{GUROBI_HOME}/include"
"/Library/gurobi901/mac64/include"
"C:\\dev\\gurobi901\\win64\\include"
)
set(SEARCH_PATHS_FOR_LIBRARIES
"$ENV{GUROBI_HOME}/lib"
"/Library/gurobi901/mac64/lib"
"C:\\dev\\gurobi901\\win64\\lib"
)
find_path(GUROBI_INCLUDE_DIR gurobi_c++.h
PATHS ${SEARCH_PATHS_FOR_HEADERS}
)
find_library( GUROBI_C_LIBRARY
NAMES gurobi90 gurobi95 libgurobi
PATHS ${SEARCH_PATHS_FOR_LIBRARIES}
)
find_library( GUROBI_CXX_LIBRARY_DEBUG
NAMES gurobi_c++ gurobi_c++mdd2017
PATHS ${SEARCH_PATHS_FOR_LIBRARIES}
)
find_library( GUROBI_CXX_LIBRARY_RELEASE
NAMES gurobi_c++ gurobi_c++md2017
PATHS ${SEARCH_PATHS_FOR_LIBRARIES}
)
# setup header file directories
set(GUROBI_INCLUDE_DIRS ${GUROBI_INCLUDE_DIR})
# setup libraries files
set(GUROBI_LIBRARIES
debug ${GUROBI_CXX_LIBRARY_DEBUG}
optimized ${GUROBI_CXX_LIBRARY_RELEASE}
${GUROBI_C_LIBRARY}
)
endif()
# Check that Gurobi was successfully found
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_INCLUDE_DIRS)
find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_LIBRARIES)
# Hide variables from CMake-Gui options
mark_as_advanced(GUROBI_LIBRARIES GUROBI_INCLUDE_DIRS GUROBI_INCLUDE_DIR)
2.2 修改CMakeLists
include(./FindGUROBI.cmake)
include_directories(${GUROBI_INCLUDE_DIRS})
include_directories(
${GUROBI_INCLUDE_DIRS}
)
add_executable(controller
src/flocking_control.cpp
)
target_link_libraries(controller
${catkin_LIBRARIES}
${GUROBI_LIBRARIES}
)