C 使用c库,其中包含标题文件中定义的全局变量
C++ using C library with global variables defined in the header file
我的目的是将用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程序中。标题和程序之间的冲突变量可能是造成此的原因。
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 崇高的林特与叮当在标题中找不到头文件
- 库设计混乱.. "public"/"private"(模板)标题,库文件..?
- 包括没有标题的文件
- Inline STD :: MUTEX在标题文件中
- 由于QT MOC,将标题包括在标题文件中
- C 在标题文件中创建具有结构定义的结构的队列
- 似乎无法将我的标题文件与我的CPP文件连接到链接列表中
- 标题文件如何参考源文件C
- 如何将用户下载的标头文件添加到系统中默认情况下的标题文件列表
- C 在两个标题文件中包括一个类
- 默认的车身或标题文件中的C 定义
- 向前删除标题文件中其他名称空间的自定义类型
- C 在多个源文件中包含具有同一类实现的不同标题文件
- 为什么将函数体放置在单独的源代码文件而不是标题中的C/C 中
- C 11在标题文件中未识别
- 如何使用多个测试.cpps在标题文件中使用instantiate_test_case_p
- 标题文件中大规模转发声明类别的风险是什么?
- 对于为什么放置在标题文件中很重要的原因感到困惑
- 无法包装标题文件中定义的简单类