+ -
当前位置:首页 → 问答吧 → C++ Static Lib 链接问题

C++ Static Lib 链接问题

时间:2010-07-22

来源:互联网

Static Lib 里面的代码:

MsgDefs.h:
  1. #pragma once

  2. #include <map>
  3. #include <string>

  4. typedef std::pair<std::string, int> MsgDefKey;
  5. typedef std::map<MsgDefKey, std::string> MsgDefs;
  6. extern MsgDefs gHSVFMsgDefs;
  7. extern int fake_link_var;
  8. int fake_link_func();

  9. #define MSG_DEF(version, msg_id, msg) \
  10.         namespace version##_##msg_id##_def \
  11.         { \
  12.                 MsgDefs::key_type key(#version, msg_id); \
  13.                 struct DefsAppender \
  14.                 { \
  15.                         DefsAppender() \
  16.                         { \
  17.                         gHSVFMsgDefs.insert(MsgDefs::value_type(key, msg)); \
  18.                         } \
  19.                 } defs_appender; \
  20.         }
复制代码
Version1.cpp:
  1. #include "stdafx.h"
  2. #include "MsgDefs.h"
  3. #include <string>

  4. using namespace std;

  5. MsgDefs gHSVFMsgDefs;
  6. const std::string V1_1 = "A1";
  7. MSG_DEF(V1, 1, V1_1)
复制代码
Version2.cpp:
  1. #include "stdafx.h"
  2. #include "MsgDefs.h"
  3. #include <string>

  4. using namespace std;
  5. int fake_link_var = 0;
  6. int fake_link_func()
  7. {
  8.         return 0;
  9. }

  10. const std::string V2_1 = "B1";
  11. MSG_DEF(V2, 1, V2_1)
复制代码
主程序代码:
  1. // LinkTest.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include "MsgDefs.h"
  5. #include <iostream>

  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.         //fake_link_var();
  9.         //fake_link_var++;
  10.         std::cout << gHSVFMsgDefs.size() << std::endl;
  11.         return 0;
  12. }
复制代码
问题: 注释掉fake_link_var那两行,输出结果为1.  启用任意一行代码后,结果为2.  求达人赐教更好的解决方案.

作者: srdgame   发布时间: 2010-07-22

如果代码都是放在主程序中的话就没有任何问题(输出结果为2)

作者: srdgame   发布时间: 2010-07-22