对于静态Win32库,如何检测是否设置了任何"使用MFC"选项?
即
#ifdef ---BuildingForMFC---
....
#else
...
#endif
Run Code Online (Sandbox Code Playgroud) 如何在C++中查看编译器生成的重载函数的受损名称?我正在使用VC9,但欢迎其他编译器的答案.
编辑:我发现这里的所有答案都很有用.接受我最喜欢的那个.
在VS2005 SP1的Debug配置中编译的以下代码显示了两条带有"ITERATOR LIST CORRUPTED"通知的消息.
代码片段
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0
#include <sstream>
#include <string>
int main()
{
std::stringstream stream;
stream << "123" << std::endl;
std::string str = stream.str();
std::string::const_iterator itFirst = str.begin();
int position = str.find('2');
std::string::const_iterator itSecond = itFirst + position;
std::string tempStr(itFirst,itSecond); ///< errors are here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器或标准库中的错误吗?
我正在开发一个用VC9构建的应用程序,我遇到了一个我不完全理解的警告:为什么在构造函数的右括号上有一个"无法访问的代码"警告?
重现问题的最小测试用例是:
__declspec(noreturn) void foo() {
// Do something, then terminate the program
}
struct A {
A() {
foo();
} // d:\foo.cpp(7) : warning C4702: unreachable code
};
int main() {
A a;
}
Run Code Online (Sandbox Code Playgroud)
必须使用/ W4编译才能触发警告.或者,您可以使用/ we4702进行编译,以强制检测此警告时出错.
d:\>cl /c /W4 foo.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
foo.cpp
d:\foo.cpp(7) : warning C4702: unreachable code
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下,究竟是什么在这里无法到达? 我最好的理论是它是析构函数,但我想要一个确定的答案.
如果我想让这段代码警告清洁,我该如何实现呢? 我能想到的最好的是将其转换为编译时错误.
struct A {
private:
A(); // No, you can't construct …Run Code Online (Sandbox Code Playgroud) 我对将std::string密钥和大struct值放入容器之类所涉及的堆和按值与引用语义有点混淆boost::interprocess::map.
这是我的情况,以及我正在使用的一些typedef:
typedef std::string AreaKeyType;
typedef DATA_AREA_DESC AreaMappedType; // DATA_AREA_DESC is a big struct.
typedef std::pair<const AreaKeyType, AreaMappedType> AreaValueType;
typedef boost::interprocess::allocator<AreaValueType, boost::interprocess::managed_shared_memory::segment_manager> AreaShmemAllocator;
typedef boost::interprocess::map<AreaKeyType, AreaMappedType, std::less<AreaKeyType>, AreaShmemAllocator> AreaMap;
Run Code Online (Sandbox Code Playgroud)
这是我如何插入AreaValueType(这是std :: pair的typedef):
AreaValueType A(areaKey, arearec);
anAreaMap->insert(A);
Run Code Online (Sandbox Code Playgroud)
我相信上面的代码将我在本地(非共享内存)堆栈上的std :: pair复制到共享内存区域.我可以在boost :: interprocess :: map中获取该共享内存区域的句柄,还是仅限于将该记录整回并整个存储?(换句话说,我可以将类似结构的东西存储到boost进程间映射中,然后更新该记录中的单个字节,或者我必须通过替换DATA_AREA_DESC结构中的所有字节来更新整个记录,全新字节).
进一步澄清:
我有一个普通的旧ANSI C DLL导出api,内部使用C++和Boost :: interprocess :: map.该函数应该在地图中创建一个项目,然后返回一个句柄.如何在boost :: interprocess :: map中插入一些东西,然后将一个句柄返回给非C++用户,最好是强制转换为void*或者unsigned long?我所能做的就是通过查找std :: string键值从共享内存中获取内容,并将新记录写入内存.我希望能够保持对共享内存对象的引用.
如果我不能直接这样做,我将如何间接地做到这一点?我想我可以保持一个非共享内存的std ::向量,并分配一个非共享内存的std :: string持有areaKey,这是一个std :: string的值,然后做的投void*项目返回std::string然后使用它从共享内存区域中获取记录.对于一些如此基本的东西来说,这似乎都是非常必要的工作.也许boost :: interprocess …
这个问题与"如何跨VS版本制作一致的dll二进制文件"有关.
虽然静态函数可以用类似的东西来处理
// Header file
class DLL_API Foo
{
int init();
}
extern "C"
{
int DLL_API Foo_init();
}
// Implementation file
int Foo_init()
{
return Foo::init();
}
Run Code Online (Sandbox Code Playgroud)
使用非静态方法并不容易.
据我了解,Chris Becke建议使用类似COM的接口对我没有帮助,因为接口成员名称仍然会被装饰,因此无法从使用不同编译器创建的二进制文件中访问.我在那儿吗?
唯一的解决方案是使用处理程序写一个C风格的DLL接口到对象还是我错过了什么?在这种情况下,我想,我可能会更少的努力直接使用包装的C库.
我有这个代码
typedef struct
{
const char* fooString;
const bool fooBool;
}fooStruct;
Run Code Online (Sandbox Code Playgroud)
而这个初始化程序:
static const fooStruct foo[] =
{
{"file1", true},
{"file2", false},
....
};
Run Code Online (Sandbox Code Playgroud)
使用此代码,我在VS2008中有3个警告:
error C2220: warning treated as error - no 'object' file generated
warning C4510: '<unnamed-tag>' : default constructor could not be generated
warning C4512: '<unnamed-tag>' : assignment operator could not be generated
warning C4610: struct '<unnamed-tag>' can never be instantiated - user defined constructor required
Run Code Online (Sandbox Code Playgroud) 我知道您可以使用a #pragma来禁用特定文件中的警告,但我想"关闭"整个VC++ 2008项目的某个警告.
我找到了一个选项Configuration Properties->C/C++->Advanced->Disable Specific Warnings但是当我输入数字警告代码并重新编译有问题的.cpp文件时,仍然会生成警告.
compiler-warnings visual-studio-2008 visual-c++-2008 visual-c++
我有一个使用本机 C++ COM 对象的 C# 程序。Visual Studio 生成一个互操作程序集,其中包含 COM 对象中类型的包装器。每次我重新编译 C# 程序互操作程序集时都有版本 1.0.0.0。
这对安装程序不利 - 有时我们扩展 COM 对象接口(在某些接口的末尾添加新方法),因此必须更改互操作程序集。当安装程序尝试更新现有安装时,它认为互操作程序集没有改变(因为它仍然有 1.0.0.0 版)并跳过更新它并且程序不起作用。
如何控制分配给互操作程序集的版本号?
我迫切需要你的帮助。
我尝试使用 Visual C++ 2008 编译器在 Windows 上静态编译 poppler 库(特别是 qt4)。为了完成这个任务,我还需要静态编译一堆其他库作为 poppler 的依赖项。当我最终生成 poppler 的静态版本时,我在构建应用程序时遇到链接错误:
error LNK2019: unresolved external symbol "__declspec(dllimport)...
Run Code Online (Sandbox Code Playgroud)
我已经添加了新的包含路径并链接了 poppler-qt4.lib 但我仍然收到错误。寻找解决方案我在 stackoverflow 中找到了这个讨论
根据这些信息,我查看了库的包含文件(poppler 的依赖项,如 zlib、libpng、cairo 等),我发现在各种情况下,它们没有预处理器指令来指定静态版本图书馆。静态指令示例(openjpeg.h):
#if defined(OPJ_STATIC) || !(defined(_WIN32) || defined(WIN32) || defined(__WIN32__))
# define OPJ_API
# define OPJ_CALLCONV
#else
# define OPJ_CALLCONV __stdcall
# ifdef OPJ_EXPORTS
# define OPJ_API __declspec(dllexport)
# else
# define OPJ_API __declspec(dllimport)
# endif /* OPJ_EXPORTS */
#endif /* !OPJ_STATIC || !WIN32 */
Run Code Online (Sandbox Code Playgroud)
没有静态指令的示例(jpeg lib …
visual-c++-2008 ×10
c++ ×7
visual-c++ ×5
.net ×1
boost ×1
c# ×1
constructor ×1
dllexport ×1
dllimport ×1
interop ×1
mfc ×1
poppler ×1
static ×1
versioning ×1
visual-c++-6 ×1
windows ×1