为什么 c++ 在包含其目录时无法定义对 gurobi 函数的引用?

Why does c++ cannot define references to gurobi's functions while its directories are included?

提问人:mdslt 提问时间:10/24/2022 最后编辑:mdslt 更新时间:10/24/2022 访问量:73

问:

我正在尝试在 Windows 952 上安装 gurobi 10 for (CLion)。我有一个有效的 gurobi 许可证(它适用于 Java 和 Python),但是,我无法在 .我在互联网上寻找解决方案并找到了几个主题,但没有一个有解决方案,尤其是对于 Windows 用户。 我创建了如下内容:c++c++CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(CHEO)
set(CMAKE_CXX_STANDARD 20)
include_directories(C:/gurobi952/win64/include)
add_executable(CHEO main.cpp)

另外,我正在初始化 gurobi 主文件,如下所示:

#include "iostream"
#include "gurobi_c++.h"
using namespace std;

int
main(int   argc,
     char *argv[])
{
    try {
    cout << "Hello" << endl;
        // Create an environment
        GRBEnv env = GRBEnv(true);
    } catch(...) {
        cout << "Exception during optimization" << endl;
    }

    return 0;
}

但是,我遇到一个错误,说:

[1/2] Building CXX object CMakeFiles/CHEO.dir/main.cpp.obj
[2/2] Linking CXX executable CHEO.exe
FAILED: CHEO.exe 
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.4\bin\mingw\bin\G__~1.EXE -g  CMakeFiles/CHEO.dir/main.cpp.obj -o CHEO.exe -Wl,--out-implib,libCHEO.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2022.2.4\bin\mingw\bin/ld.exe: CMakeFiles/CHEO.dir/main.cpp.obj:C:/Users/USERNAME/CLionProjects/CHEO/main.cpp:12: undefined reference to `GRBEnv::GRBEnv(bool)'
C:\Program Files\JetBrains\CLion 2022.2.4\bin\mingw\bin/ld.exe: CMakeFiles/CHEO.dir/main.cpp.obj: in function `main':
C:/Users/USERNAME/CLionProjects/CHEO/main.cpp:48: undefined reference to `GRBEnv::~GRBEnv()'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

此外,我还尝试了此处提供的解决方案(gurobi 的支持网站)。但是,一旦我将建议的文件包含在我的项目(和)的目录中,我在编译文件时会遇到以下错误:CMakeLists.txtFindGUROBI.cmakeCMakeLists.txt

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_LIBRARY
    linked by target "gurobi-template" in directory C:/Users/USERNAME/CLionProjects/CHEO
    linked by target "gurobi-template" in directory C:/Users/USERNAME/CLionProjects/CHEO

对于Windows用户来说,这个问题的解决方案是什么?感谢您的耐心等待,因为我是新手。c++

C++ Windows 未定义引用 gurobi

评论

0赞 ChrisMM 10/24/2022
这回答了你的问题吗?什么是未定义的引用/未解析的外部符号错误,如何解决?
0赞 ChrisMM 10/24/2022
您只添加了 ,您需要实际链接库。include_directory
0赞 Mat 10/24/2022
support.gurobi.com/hc/en-us/articles/......
0赞 mdslt 10/24/2022
@Mat,您发布的链接没有帮助(如果我没有错过任何东西)。我在我的项目目录中包含此链接中提供的两个文件,并在我的 CLion 终端中复制了命令。但是,他们没有工作。我会用这些信息更新我的帖子。
0赞 Mat 10/24/2022
您的 CMakeList 文件几乎缺少它们提供的所有内容。

答: 暂无答案