C 使用c库,其中包含标题文件中定义的全局变量

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

本文关键字:文件 标题 定义 全局变量 包含 使用      更新时间:2023-10-16

我的目的是将用C编写的LKH TSP算法包括在我的C 项目中。

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

来源:http://www.akira.ruc.dk/~keld/research/lkh/lkh/lkh-2.0.7.7.tgz

首先,我开始写一个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)

之后,我想实现两个使用库的功能。第一个只是读取TSPLIB的文件,然后将值复制到我自己的地图结构中。第二个应该通过使用我的地图结构而不阅读任何文件来解决TSP问题。(通常只能通过读取文件来使用该程序,因此这是包装器部分)

现在我的问题:所有变量均在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;

现在它们用于 *.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);
..........
..........

在此示例中,变量Tracelevel,InitialTouralGorithm和FirstNode用作全局变量。

我的问题:当我将LKH.H纳入班级时,我会有很多副作用。执行方法后,全局变量已更改。我想将所有这些都重置为执行下一个方法,而无需任何先前设置的值。我写了几个测试用例,而GTEST方法具有奇怪的效果,这是我认为由全球变量行为引起的。

和这里的包装文件:

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
    }

解决该问题并获得两种方法的副作用的最简单方法是什么?

@Mason Watmough:

是的,但是问题是我不会自己定义它们。我必须使用

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

,该定义和声明变量(请参阅C程序中的LKH.H)

并且没有冲突或链接错误!这些变量仅在LKH.CPP中仅存在一次。库liblkh.a也没有此变量,因为它们仅在lkh.h。

上定义

如果您的全局变量在标题文件以及C程序中定义,则需要选择一个或另一个。要么从C程序中删除全局变量定义,然后将其全部放入标头文件中,要么弄清楚如何将标头文件正确放入所有不同的C程序中。标题和程序之间的冲突变量可能是造成此的原因。