首页 > 解决方案 > 如何使用在 DLL 上定义的全局 const 变量

问题描述

我有下一个文件common/const.h

#ifndef COMMON_CONST_H
#define COMMON_CONST_H

#if defined _WIN32 || defined _WIN64
#ifdef PCOM_EXPORTS
#define PCOM_WINAPI __declspec(dllexport)
#else
#define PCOM_WINAPI __declspec(dllimport)
#endif // PCOM_EXPORTS
#endif // _WIN32

namespace common {

extern PCOM_WINAPI const char DIR_SEPARATOR;

}

#endif /* COMMON_CONST_H */

common/const.cpp

#include "common/const.h"

namespace common {

#if defined _WIN32 || defined _WIN64
const char DIR_SEPARATOR = '\\';
#else
const char DIR_SEPARATOR = '/';
#endif

}

我生成它的 DLL 文件没有问题。但是当我想使用另一个库中的这个DIR_SEPARATOR变量时,我得到:

Util.obj:错误 LNK2001:未解析的外部符号“char const common::DIR_SEPARATOR”:致命错误 LNK1120:1 个未解析的外部符号

Util.cpp(dll客户端)代码是:

#include "common/const.h"
...
namespace db {
...
shared_ptr<iptree> Util::loadIniFile(const string& filePath) {
  string file = filePath;
  replace(file.begin(), file.end(), '/', prompt::common::DIR_SEPARATOR);
  shared_ptr<iptree> tree(new iptree);
  ...
}

}

我的问题是:

我只想设置一次DIR_SEPARATOR值。这种情况下可以吗?我错过了什么吗?

提前致谢。

标签: c++dllporting

解决方案


推荐阅读