C ++ Eclipse中未定义的(错误)引用,但在Visual Studio 2015中工作 [英] Undefined reference to (error) in C++ Eclipse but working in Visual Studio 2015

查看:944
本文介绍了C ++ Eclipse中未定义的(错误)引用,但在Visual Studio 2015中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 AMPL 与C / C ++集成/ products / api /rel =nofollow> AMPL-API 在Eclipse Mars 2.0中的Windows-7上。我在Eclipse中创建了一个Makefile项目,它使用MinGW CC编译其示例目录中给出的firstexample代码。

I am trying to integrate AMPL with C/C++ using AMPL-API on Windows-7 in Eclipse Mars 2.0. I created a Makefile project in Eclipse which uses MinGW CC to compile the firstexample code given in their example directory.


firstexample.cpp:

firstexample.cpp:



#include <iostream>
#include "ampl/ampl.h"

using namespace std;

    int main() {
        ampl::AMPL ampl;

        // Read the model and data files.
        std::string modelDirectory = "models";
        ampl.read(modelDirectory + "/diet/diet.mod");
        ampl.readData(modelDirectory + "/diet/diet.dat");

        // Solve
        ampl.solve();

        // Get objective entity by AMPL name
        ampl::Objective totalcost = ampl.getObjective("total_cost");
        // Print it
        std::cout << "Objective is: " << totalcost.value() << std::endl;
        // Get objective entity by AMPL name
        ampl::Objective totalcost = ampl.getObjective("total_cost");
        // Print it
        std::cout << "Objective is: " << totalcost.value() << std::endl;

        // Reassign data - specific instances
        ampl::Parameter cost = ampl.getParameter("cost");
        cost.setValues(new Tuple[2]{ ampl::Arg("BEEF"),  ampl::Arg("HAM")}, new Arg[2]{ 5.01, 4.55 },
                                   2);
        std::cout << "Increased costs of beef and ham." << std::endl;

        // Resolve and display objective
        ampl.solve();
        std::cout << "New objective value: " << totalcost.value() << std::endl;

        // Reassign data - all instances
        ampl::Arg elements[8]{ 3, 5, 5, 6, 1, 2, 5.01, 4.55 };
        cost.setValues(elements);

        std::cout << "Updated all costs." << std::endl;

        // Resolve and display objective
        ampl.solve();
        std::cout << "New objective value: " << totalcost.value() << std::endl;

        // Get the values of the variable Buy in a dataframe object
        Variable buy = ampl.getVariable("Buy");
        ampl::DataFrame df;
        df = buy.getValues();
        // Print them
        df.print();
        ampl::DataFrame df2;
        // Get the values of an expression into a DataFrame object
        df2 = ampl.getData("{j in FOOD} 100*Buy[j]/Buy[j].ub");
        // Print them
        df2.print();
}




以下是我的Makefile:

Following is my Makefile:



CC = g++

CFLAGS = -O2 -g -Wall -fmessage-length=0

INCLUDES = -I "C:\\Local\\AMPL\\AMPL32\\amplapi32\\include"

OBJS = AMPL.o

LFLAGS = -L "C:\\Local\\AMPL\\AMPL32\\amplapi32\\lib"

LIBS =  -lampl1.2.2 

TARGET = AMPL.exe

$(TARGET):  $(OBJS)
    $(CC) $(CFLAGS) $(INCLUDES) -o $(TARGET) $(OBJS) $(LFLAGS) $(LIBS)

AMPL.o: AMPL.cpp
    $(CC) $(CFLAGS) $(INCLUDES) -c AMPL.cpp

all:    $(TARGET)

clean:
    rm -f $(OBJS) $(TARGET)



我已经将所需的dll文件(libampl1.2.2.dll)的路径添加到环境变量。我可以在Visual Studio 2015上编译和执行代码,并进行两个小的更改:

I have added path of required dll files (libampl1.2.2.dll) to the environment variables. I am able to compile and execute code on Visual Studio 2015 with two minor changes:


  • 不使用Makefile(它是Win32控制台应用程序)

  • 在firstexample.cc中添加 #includestdafx.h

  • Without using Makefile (It is a Win32 Console Application)
  • Adding #include "stdafx.h" in firstexample.cc

但是当我在Eclipse中执行相同的代码时,它给我以下错误:

However when I execute the same code in Eclipse, it gives me following error:

src\AMPLTesting.o: In function `ZN4ampl8internal11deleteTupleERNS0_5TupleE':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:24: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_DeleteArrayEPKNS0_7VariantE'
src\AMPLTesting.o: In function `ZN4ampl8internal12TupleBuilderC1Ej':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:35: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_CreateArrayEjPNS0_16ErrorInformationE'
collect2.exe: error: ld returned 1 exit status

我不知道是什么问题?我缺少一些命令行选项在Makefile或没有添加任何特定的库?请帮我这个。

I am not sure what is the problem? Am I missing some command line option in the Makefile or not adding any specific library? Please help me with this.

推荐答案

C ++ API的测试版目前仅支持Windows上的MSVC。在将来的版本中将添加对其他编译器的支持。

The beta version of the C++ API only supports MSVC on Windows at the moment. Support for other compilers will be added in future releases.

这篇关于C ++ Eclipse中未定义的(错误)引用,但在Visual Studio 2015中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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