C++ using C library with global variables defined in the header file -


my intention include lkh tsp algorithm, written in c, c++ project.

lkh: http://www.akira.ruc.dk/~keld/research/lkh/

sources: http://www.akira.ruc.dk/~keld/research/lkh/lkh-2.0.7.tgz

first of started write cmakelists.txt create library not contain lkhmain.c.

file(glob_recurse sources src/*.c) file(glob_recurse headers src/include/*.h)  include_directories(${cmake_current_source_dir}/src/include)  set(lib_sources ${sources}) list(remove_item lib_sources ${cmake_current_source_dir}/src/lkhmain.c)  add_library(lkh     ${lib_sources} ) target_link_libraries(lkh m) 

after want implement 2 function uses library. first 1 reads file of tsplib , copies values own map structure. second 1 should solve tsp problem using map structure without reading file. (the program used reading file wrapper part)

now problem: variables defined in lkh.h , used implementations. e.g.

lkh.h

int tracelevel; /* specifies level of detail of output                given during solution process.                value 0 signifies minimum amount of                output. higher value more                information given */ node *firstnode;        /* first node in list of nodes */ int initialtouralgorithm; 

and used in *.c classes.

greedytour.c

#include "lkh.h"   gaintype greedytour() {     node *from, *to, *first, *last = 0, **perm;     int count, i;     double entrytime = gettime();      if (tracelevel >= 1) {         if (initialtouralgorithm == boruvka)             printff("boruvka = ");         else if (initialtouralgorithm == greedy)             printff("greedy = ");         else if (initialtouralgorithm == nearest_neighbor)             printff("nearest-neighbor = ");         else if (initialtouralgorithm == quick_boruvka)             printff("quick-boruvka = ");     }     cost = 0;     edgesinfragments = 0;     = firstnode;     {         from->degree = 0;         from->tail = from;         from->mark = 0;         from->next = from->suc;         from->pred = from->suc = 0;     }     while ((from = from->next) != firstnode);  .......... .......... 

in example variables tracelevel, initialtouralgorithm , firstnode used global variables .

my problem: when include lkh.h class have lot of side effects. after executing method, global variables have changed. want reset of them execute next method without set values. wrote several test cases , gtest methods has strange effects in opinion caused global variables behavior.

and here wrapper files:

lkh.h

#include "model/map.h"  namespace ttp {      class lkhwrapper {      public:          mapptr createmap(std::string);          int *calc(mapptr map);      };  } 

lkh.cpp

#include <fstream> #include <limits> #include "lkh.h"  extern "c" { #include "lkh.h" #include "genetic.h" #include "heap.h" }  namespace ttp {      mapptr lkhwrapper::createmap(std::string pathtofile) {         ...         [change global variables]         ...         return map;      }      int* lkhwrapper::calc(mapptr map) {         ...         [change global variables]         ...         return besttour     } 

what easiest way solve problem , side effect free implementation of 2 methods?

@mason watmough:

yeah problem not define them myself. have use

 extern "c"      {      #include "lkh.h"      #include "genetic.h"      #include "heap.h"      }

and defines , declares variables (see lkh.h @ c program)

and there no conflict or linking error! variables exists once @ lkh.cpp including lkh.h. library liblkh.a not have variables because defined @ lkh.h.

if global variables defined in header file c programs, need choose 1 or other. either delete global variable definitions c programs , put in header files, or figure out how put header file different c programs. conflicting variables between header , program caused this.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -