jeudi 19 octobre 2023

Problem with linking library in c++ to create exe

I have some problems with compiling the code in c++ using Makefile in a linux server (ubuntu). I use some different classes and in SaaS_Problem.cpp I use gurobi optimization library to solve the problem and it seems that I cannot link the library into my code while apparently everything is set appropriately. SaaS_Problem.cpp is briefly as follows (I didn't bring whole the code because it is too long):

#include "SaaS_Problem.hpp"
#include "SaaS.hpp"
#include "application.hpp"
#include "WS.hpp"
#include "/home/Library/gurobi911/linux64/include/gurobi_c++.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <memory>

std::vector<std::pair<double, double>> SaaS_Problem::set_param_B ( WS & w, application& app)
{
  std::vector <std::pair<double, double>> result; // we will return a vector of paris, we will have as pair.first the nominal value B_bar_a_w_l and as pair.second B_dev
  double A_a_w = w.get_R_bar_a_w() - w.get_D_a_w(); // first compute A_a_w (application a, web service w)
  std::vector<double> B;
  double B_dev = 0.0;
  for( unsigned l = 0; l < app.get_size(); l++) //cycle among all the web services of the application
  {
    auto web_service_l = app.get_web_services()[l];
    double B_a_x_l_bar = A_a_w*w.get_mu_a_w()/((1-A_a_w*w.get_mu_a_w())*web_service_l.get_mu_a_w());
    B_dev = compute_B_dev( w, web_service_l, B_a_x_l_bar );


    //std::cout << "B_bar = "<< B_a_x_l_bar << '\n'; // debugging
    //std::cout << "B_dev = "<< B_dev << '\n'; //debugging


    result.push_back(std::make_pair( B_a_x_l_bar ,  B_dev));
  }
  return result;
}

void SaaS_Problem::solve(void)
{
  try
  {
    rejected_requests = 0;
    rejected_requests_vec.resize(0);
    GRBEnv env = GRBEnv(); // create a gurobi environment

    GRBModel model = GRBModel(env); //create an empty model

    GRBLinExpr obj = 0.0; // create a linear expression to construct my objective function

    // Create variables

    GRBLinExpr total_on_flat = 0;   // create a linear expression which will be the sum of all on_flat VMs
    GRBLinExpr total_VM = 0;        // create a linear expression which will be the sum of all VMs
    std::vector<GRBVar> x_v;        // here we store the variables X_a_w because we need them for the computation of the parameters B

    std::vector<GRBVar> alpha_v;    // here we store all the values of alpha, we need it just for debugging
    std::vector<GRBVar> beta_v;     // here we store all the values of beta, we need it just for debugging
    std::vector<GRBVar> y_v;        // here we store the variables the number of rejacted requests

    //std::vector<GRBLinExpr> bx; // for debugging

    double total_on_spot = 0.0;

    // setting the variables and the objective function
    for(unsigned i = 0; i < s -> get_size(); i++)                                      // cicle over all the applications of the SaaS
    {

      auto app = s -> get_applications()[i];                                           // ith application

      total_on_spot += s -> get_on_spot(app);

      std::string r_a = "r" + std::to_string(i);                                       // create the name for the variables
      std::string d_a = "d" + std::to_string(i);

      GRBVar r  = model.addVar(0.0, GRB_INFINITY, 0.0  , GRB_CONTINUOUS, r_a);         // creating r_a
      GRBVar d  = model.addVar(0.0, GRB_INFINITY, 0.0  , GRB_CONTINUOUS, d_a);         // creating d_a

      obj += rho * r;                                                                  // adding r_a to the objecting funcion
      obj += delta * d;                                                                // adding d_a to the objecting funcion

      total_on_flat += r;                                                              // updating the sum of all the on flat VMs
      total_VM += ( r + d );                                                           // updating the sum of all the VMs


     for(unsigned j = 0; j < app.get_size(); j++)                                                  // cicle over all the WSs of the application i
      {

        auto web_service = app.get_web_services()[j];                                               // j-th WS of the i-th application

        GRBVar y = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS);                            // creating y_a_w

        obj += (T * web_service.get_nu_a_w() * y);                                                  // adding it to the objecting funcion

        GRBVar x = model.addVar(0.0, GRB_INFINITY, 0.0  , GRB_CONTINUOUS);                          // creating X_a_w

        model.addConstr( x >= web_service.get_lambda_a_w());                                       // X_a_w >= lambda_a_w
        model.addConstr( y + x >= web_service.get_LAMBDA_a_w() );                                   // X_a_w + y_a_w >= LAMBDA_a_w


        y_v.push_back(y);                                                                           // store y_a_w in y_v
        x_v.push_back(x);                                                                           // store X_a_w in x_v

      }

      .

Makefile is as follows:

CXXFLAGS += -Wall -std=c++11 -I/home/Library/gurobi911/linux64/include -L/home/Library/gurobi911/linux64/lib  -lgurobi_c++ -lgurobi91

EXE = exe

OBJS = main.o Game.o Set_System.o SaaS_Problem.o SaaS.o application.o WS.o IaaS.o IaaS_Problem.o

.DEFAULT_GOAL = all

main.o Game.o Set_System.o SaaS_Problem.o SaaS.o application.o WS.o : WS.hpp
main.o Game.o Set_System.o SaaS_Problem.o SaaS.o application.o : application.hpp
main.o Glpthreadame.o Set_System.o SaaS_Problem.o SaaS.o : SaaS.hpp
main.o Game.o Set_System.o SaaS_Problem.o : SaaS_Problem.hpp
main.o Game.o Set_System.o IaaS_Problem.o IaaS.o: IaaS.hpp
main.o Game.o Set_System.o IaaS_Problem.o: IaaS_Problem.hpp
main.o Game.o Set_System.o : Set_System.hpp
main.o Game.o : Game.hpp

all : build

.PHONY : all build clean distclean

build : $(EXE)

$(EXE) : $(OBJS)
    $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $^ $(OUTPUT_OPTION) -I/home/Library/gurobi911/linux64/include -L/home/Library/gurobi911/linux64/lib -lgurobi_c++ -lgurobi91

clean:
    $(RM) *.o

distclean: clean
    $(RM)$(EXE)
    $(RM)*~

and .bashrc file is:

export GUROBI_HOME="/home/Library/gurobi911/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"
export GRB_LICENSE_FILE="/home/Library/gurobi.lic"
export PATH="${PATH}:${GUROBI_HOME}/include"

when I use make to create exe file I get following error:

g++ -Wall -std=c++11 -I/home/Library/gurobi911/linux64/include -L/home/Library/gurobi911/linux64/lib -lgurobi_c++ -lgurobi91   main.o Game.o Set_System.o SaaS_Problem.o SaaS.o application.o WS.o IaaS.o IaaS_Problem.o -o exe -I/home/Library/gurobi911/linux64/include -L/home/Library/gurobi911/linux64/lib -lgurobi_c++ -lgurobi91
SaaS_Problem.o: In function `SaaS_Problem::solve()':
SaaS_Problem.cpp:(.text+0x1523): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x157e): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x17c2): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x18e1): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x19af): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x1abb): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x1c71): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x1d48): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x1ee3): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x21a5): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x23c3): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x2505): undefined reference to `GRBModel::addConstr(GRBTempConstr const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
SaaS_Problem.cpp:(.text+0x28bb): undefined reference to `GRBModel::getVarByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SaaS_Problem.cpp:(.text+0x2920): undefined reference to `GRBModel::getVarByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SaaS_Problem.cpp:(.text+0x33fc): undefined reference to `GRBException::getMessage[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
Makefile:26: recipe for target 'exe' failed
make: *** [exe] Error 1

How can I solve this problem?

Aucun commentaire:

Enregistrer un commentaire