成功的Makefile失败,并显示“相同".CMake in CLion [英] Successful Makefile fails with "identical" CMake in CLion

查看:209
本文介绍了成功的Makefile失败,并显示“相同".CMake in CLion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CLion在Mac OS 10.13.1(macOS High Sierra)上与IBM ILOG CPLEX 12.7.1一起使用.这是一段非常简单的C ++代码.

I am trying to use CLion to work with IBM ILOG CPLEX 12.7.1 on my Mac OS 10.13.1 (macOS High Sierra). Here is a very simple piece of C++ code.

#include <iostream>
#include <ilcplex/ilocplex.h>
using namespace std;
int main() {
    std::cout << "Hello, World!" << std::endl;
    IloEnv env;
    x: IloCplex cplex(env);
    std::cout << env.getVersion() << endl;

    return 0;
}

我的 Makefile 中的以下内容.

SYSTEM = x86-64_osx
LIBFORMAT = static_pic
CPLEXDIR = /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex
CONCERTDIR = /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/concert
# ---------------------------------------------------------------------
# Compiler selection 
# --------------------------------------------------------------------- 
CCC = clang++ -O0
# ---------------------------------------------------------------------
# Compiler options 
# ---------------------------------------------------------------------
CCOPT = -m64 -O -fPIC -fexceptions -DNDEBUG -DIL_STD -stdlib=libc++
# ---------------------------------------------------------------------
# Link options and libraries
# ---------------------------------------------------------------------

CPLEXBINDIR = $(CPLEXDIR)/bin/$(BINDIST)

CPLEXLIBDIR = $(CPLEXDIR)/lib/$(SYSTEM)/$(LIBFORMAT)
CONCERTLIBDIR = $(CONCERTDIR)/lib/$(SYSTEM)/$(LIBFORMAT)

CCLNDIRS  = -L$(CPLEXLIBDIR) -L$(CONCERTLIBDIR) 
CCLNFLAGS = -lconcert -lilocplex -lcplex -lm -lpthread -ldl #-framework CoreFoundation -framework IOKit -stdlib=libc++
all:
   make main

CONCERTINCDIR = $(CONCERTDIR)/include
CPLEXINCDIR = $(CPLEXDIR)/include

CCFLAGS = $(CCOPT) -I$(CPLEXINCDIR) -I$(CONCERTINCDIR) 
# ------------------------------------------------------------
main: main.o
$(CCC) $(CCFLAGS) $(CCLNDIRS) -o main main.o $(CCLNFLAGS)
main.o: ./main.cpp
$(CCC) -c $(CCFLAGS) ./main.cpp -o main.o

这是 CMakeLists.txt ,我尝试使用与 Makefile 完全相同的参数来创建它.

Here is the CMakeLists.txt, which I have tried to create it with parameters exactly as mentioned in the Makefile.

cmake_minimum_required(VERSION 3.14)
project(cplextest)
add_executable(cplextest main.cpp)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -m64 -O -fPIC -fexceptions -DNDEBUG -DIL_STD -stdlib=libc++")
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex/include/)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex/include/ilcplex)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/concert/include)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/concert/include/ilconcert)
find_library(lib1 NAMES libcplex.a PATHS /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex/lib/x86-64_osx/static_pic/)
find_library(lib2 NAMES libilocplex.a PATHS /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex/lib/x86-64_osx/static_pic/)
find_library(lib3 NAMES libcplexdistmip.a PATHS /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio1271/cplex/lib/x86-64_osx/static_pic/)
set (CMAKE_SHARED_LINKER_FLAGS "-lconcert -lilocplex -lcplex -lm -lpthread -ldl #-framework CoreFoundation -framework IOKit -stdlib=libc++")
target_link_libraries(cplextest PUBLIC ${lib1})
target_link_libraries(cplextest PUBLIC ${lib2})
target_link_libraries(cplextest PUBLIC ${lib3})

但是,尽管 Makefile 正常运行(即,我可以从终端成功运行程序),但CLion会产生以下错误消息.

However, while Makefile works flawlessly (i.e., I can successfully run my program from a terminal), CLion produces the following error messages.

====================[ Build | cplextest | Debug ]===============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/soheilmn/CLionProjects/CplexTest/cmake-build-debug --target cplextest -- -j 2
[ 50%] Linking CXX executable cplextest
Undefined symbols for architecture x86_64:
  "IloCplex::IloCplex(IloEnv)", referenced from:
      _main in main.cpp.o
  "IloCplexI::getVersion() const", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [cplextest] Error 1
make[2]: *** [CMakeFiles/cplextest.dir/all] Error 2
make[1]: *** [CMakeFiles/cplextest.dir/rule] Error 2
make: *** [cplextest] Error 2

有趣的是,如果我在C ++代码中注释掉(x)行,则可以从CLion成功运行代码.这确实让我感到困惑,因为我的理解是这两个过程都使用 clang ++ 和相同的库/头文件.任何帮助将不胜感激.(对于较长的帖子,我们深表歉意!)

Interestingly, if I comment out the line (x) in my C++ code, I can successfully run my code from CLion. This is indeed very confusing to me, because my understanding is both procedures use clang++ and the same libraries/header files. Any help will be highly appreciated. (Sorry for the long post in advance!)

谢谢!

推荐答案

特别感谢"John Zwinck"的帮助.我认为在macOS 10.13.1/CLion 2109.2.3下共享最终与IBM ILOG CPLEX 12.9完美配合的最终CMake文件是有意义的.在这里:

Special thanks go to "John Zwinck" for his help. I thought it makes sense to share the final CMake file that perfectly works with IBM ILOG CPLEX 12.9 under macOS 10.13.1 / CLion 2109.2.3. Here it is:

cmake_minimum_required(VERSION 3.14)
project(cplextest)
add_executable(cplextest main.cpp)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -m64 -O -fPIC -fexceptions -DNDEBUG -DIL_STD -stdlib=libc++")
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/cplex/include/)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/cplex/include/ilcplex)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/concert/include)
include_directories(/Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/concert/include/ilconcert)
target_link_libraries(cplextest PUBLIC /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/cplex/lib/x86-64_osx/static_pic/libcplex.a)
target_link_libraries(cplextest PUBLIC /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/cplex/lib/x86-64_osx/static_pic/libilocplex.a)
target_link_libraries(cplextest PUBLIC /Users/soheilmn/Applications/IBM/ILOG/CPLEX_Studio129/concert/lib/x86-64_osx/static_pic/libconcert.a)
set (target_link_options "-lconcert -lilocplex -lcplex -lm -lpthread -ldl -framework CoreFoundation -framework IOKit -stdlib=libc++")

这篇关于成功的Makefile失败,并显示“相同".CMake in CLion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆