简体   繁体   English

C ++使用C库在头文件中定义了全局变量

[英]C++ using C library with global variables defined in the header file

My intention is to include the LKH TSP Algorithm, which is written in C, into my C++ project. 我的意图是将用C语言编写的LKH TSP算法包含到我的C ++项目中。

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

Sources: http://www.akira.ruc.dk/~keld/research/LKH/LKH-2.0.7.tgz 资料来源: http : //www.akira.ruc.dk/~keld/research/LKH/LKH-2.0.7.tgz

First of all I started to write a CMakeLists.txt to create a library that does not contain the LKHmain.c. 首先,我开始编写CMakeLists.txt来创建一个不包含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 that I want to implement two function that uses the library. 之后,我想实现两个使用该库的函数。 The first one just reads a file of the TSPLib and copies the values into my own Map structure. 第一个只是读取TSPLib的文件,然后将值复制到我自己的Map结构中。 The second one should solve a TSP Problem by using my map structure without reading any file. 第二个应该通过使用我的地图结构而不读取任何文件来解决TSP问题。 (The program normally could only used by reading a file so this is a wrapper part) (该程序通常只能通过读取文件来使用,因此这是包装器的一部分)

Now my problem: All the variables are defined in the LKH.h and used by all the implementations. 现在我的问题是:所有变量都在LKH.h中定义,并由所有实现使用。 So eg 所以例如

LKH.h LKH.h

int TraceLevel; /* Specifies the level of detail of the output
               given during the solution process.
               The value 0 signifies a minimum amount of
               output. The higher the value is the more
               information is given */
Node *FirstNode;        /* First node in the list of nodes */
int InitialTourAlgorithm;

and now they are used in the *.c classes. 现在,它们已在* .c类中使用。

GreedyTour.c 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;
    From = FirstNode;
    do {
        From->Degree = 0;
        From->Tail = From;
        From->Mark = 0;
        From->Next = From->Suc;
        From->Pred = From->Suc = 0;
    }
    while ((From = From->Next) != FirstNode);

..........
..........

In this example the variables TraceLevel, InitialTourAlgorithm and FirstNode are used as global variables . 在此示例中,变量TraceLevel,InitialTourAlgorithm和FirstNode用作全局变量。

My problem: When I include the LKH.h to my class I have a lot of side effects. 我的问题:当我将LKH.h包含在我的课程中时,会产生很多副作用。 After executing a method, the global variables have changed. 执行方法后,全局变量已更改。 I want to reset all of them to execute to next method without any previously set values. 我想将所有它们重置为执行下一个方法,而无需任何先前设置的值。 I wrote several test cases and the gtest methods has strange effects which is in my opinion caused by the global variables behavior. 我写了几个测试用例,而gtest方法具有奇怪的效果,我认为这是由全局变量行为引起的。

And here my wrapper files: 这是我的包装文件:

lkh.h lkh.h

#include "model/map.h"

namespace ttp {

    class LKHWrapper {

    public:

        MapPtr createMap(std::string);

        int *calc(MapPtr map);

    };

}

lkh.cpp 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 is the easiest way to solve that problem and get a side effect free implementation of that two methods? 解决该问题并获得这两种方法的无副作用实现的最简单方法是什么?

@Mason Watmough: @梅森·沃特莫夫(Mason Watmough):

Yeah but the problem is I do not define them by myself. 是的,但是问题是我没有自己定义它们。 I have to use 我必须用

  extern "C" { #include "LKH.h" #include "Genetic.h" #include "Heap.h" } 

And that defines and declares the variables (see LKH.h at the C Program) 然后定义并声明变量(请参见C程序中的LKH.h)

And there is no conflict or linking error! 并且没有冲突或链接错误! The variables only exists once at the lkh.cpp by including the LKH.h. 通过包含LKH.h,变量在lkh.cpp中仅存在一次。 The library liblkh.a does also not have this variables because they are ONLY defined at the LKH.h. 库liblkh.a也没有此变量,因为它们仅在LKH.h中定义。

If your global variables are defined in your header file as well as your c programs, you will need to choose one or the other. 如果在头文件和c程序中定义了全局变量,则需要选择一个或另一个。 either delete the global variable definitions from the c programs and put it all in the header files, or figure out how to properly put the header file into all different c programs. 从c程序中删除全局变量定义,然后将其全部放入头文件中,或者弄清楚如何正确地将头文件放入所有不同的c程序中。 Conflicting variables between the header and the program is probably what caused this. 标题和程序之间的变量冲突可能是造成此问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM