C++/C在Visual Studio中的两个项目之间共享全局变量
C++/C Shareing global variable between 2 projects in Visual Studio
在VS中,我有一个包含2个项目的sln:
项目A:
A.h
#include <string>
extern bool flag;
A.cpp
#include "A.h"
bool flag = false;
void funcA()
{
int i = 0;
}
项目B:
B.h
#include <stdio.h>
B.cpp
#include "B.h"
#include "..ProjectAA.h"
void main()
{
int j = 10;
flag = true;
std::cout << j << "n" << flag ;
}
我将项目A设置为DLL,将项目B设置为EXE。
在链接中,我得到了错误:错误LNK2001:未解析的外部符号"bool flag"(?flag@@3_NA)
我应该在设置中手动指定项目B到项目A吗?
谢谢。
像这样:
A.h
#ifndef LIBA_API
#define LIBA_API __declspec(dllimport)
#endif
extern LIBA_API bool flag;
A.cpp
#define LIBA_API __declspec(dllexport)
#include "A.h"
LIBA_API bool flag = false;
void funcA()
{
int i = 0;
}
(无需更改B.h
或B.cpp
)
相关文章:
- 如何在QT中为两个项目使用单个UI
- 在 Linux 上的 Visual Studio 中链接两个项目
- 在两个项目中使用全局变量
- 在代码块中一起编译两个项目
- 一个解决方案,两个项目:如何从 Windows 应用程序调用控制台项目?C++
- QTreeWidget使用拖放在两个项目之间插入
- NetBeans - 两个项目 - 一个找不到要包含的文件 (C++)
- CMakeLists配置以链接两个C++项目
- 两个项目静态地链接同一个SQLite库会导致问题
- qtmake-两个项目/目标之间的共享资源
- C++/C在Visual Studio中的两个项目之间共享全局变量
- 两个C++项目可以相互引用吗?
- 在 Visual Studio 解决方案中链接两个项目
- Visual Studio:在两个项目的解决方案中包含.h或.cpp
- 一个链接节点,在一个节点中有两个项目
- 在std::list中合并(将两个项目融合在一起,用融合替换)项目的算法(即破坏性聚类)
- 共享了两个项目文件
- C++ 一个解决方案两个项目(EXE & DLL)链接错误
- 在两个项目之间链接时,获取虚拟函数的链接错误
- 在C++中查找两个项目之间的差异时出错