问题
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:
#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();
}
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)
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:
- Without using Makefile (It is a Win32 Console Application)
- Adding
#include "stdafx.h"
in firstexample.cc
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
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.
回答1:
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.
来源:https://stackoverflow.com/questions/37581483/undefined-reference-to-error-in-c-eclipse-but-working-in-visual-studio-2015