所以我有两个静态库定义如下:
StaticLib1
// StaticLib1.h
#pragma once
class StaticLib1
{
public:
void doSomething1();
};cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib1.h"
void StaticLib1::doSomething1()
{
pugi::xml_node node;
}StaticLib2
// StaticLib2.h
#pragma once
class StaticLib2
{
public:
void doSomething2();
};cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib2.h"
void StaticLib2::doSomething2()
{
pugi::xml_node node;
}主
#include <iostream>
#include "StaticLib1.h"
#include "StaticLib2.h"
int main(int argv, char** argc)
{
StaticLib1 staticlib1;
StaticLib2 staticlib2;
staticlib1.doSemething1();
staticlib2.doSemething2();
getchar();
return 0;
}如果我造了这个。我有很多链接错误。以下是前几个链接错误:
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(struct pugi::xml_attribute_struct *)" (??0xml_attribute@pugi@@QAE@PAUxml_attribute_struct@1@@Z) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(void)" (??0xml_attribute@pugi@@QAE@XZ) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "private: __thiscall pugi::xml_attribute_iterator::xml_attribute_iterator(struct pugi::xml_attribute_struct *,struct pugi::xml_node_struct *)" (??0xml_attribute_iterator@pugi@@AAE@PAUxml_attribute_struct@1@PAUxml_node_struct@1@@Z) already defined in StaticLib1.lib(StaticLib1.obj)
...
...现在,我了解到这个链接错误是因为StaticLib1.lib内部有一个pugixml.obj,而StaticLib2.lib中有一个pugixml.obj。但我不明白为什么这会导致pugixml签名的链接错误。为什么要定义两次呢?如果我调用staticlib1.doSomething1(),是否应该不关心pugi是否有多个定义?难道staticlib1.doSomething1()不应该处理所有这些吗?
在pugiconfig.hpp上,我有以下特定设置:
#ifndef HEADER_PUGICONFIG_HPP
#define HEADER_PUGICONFIG_HPP
#define PUGIXML_WCHAR_MODE
#define PUGIXML_HEAD_ONLY
#include "pugixml.cpp"
#endif发布于 2017-11-13 21:19:43
因此,是的,从user0042建议中,我意识到最好自己编译一个pugixml.lib,而不是在配置中使用#include "pugixml.cpp"。我正在处理遗留代码,所以这些惊喜就出现了。现在,我已经解决了我的问题,并使我的公司代码稍微干净一些。
https://stackoverflow.com/questions/47272810
复制相似问题