[英]C++ Boost::thread invoking a c function - Facing compilation error
Have to invoke a C static function as part of my boost worker thread function.The C static function works as part of other C++ code however, it fails to compile while being part of a worker thread function.
以下是我的 C++ header 文件中包含的头文件列表。
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#include <string>
#include <queue>
和 c 代码 header 开头如下
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MS_WINNT) || defined(WIN32)
#include <windows.h>
typedef BSTR ARG;
我无法控制 C 代码,但可以控制我正在调用的 C++ 代码。
我遇到的错误信息如下
错误 C2146:语法错误:缺少“;”
但是,如果我颠倒包含文件的顺序,我会收到不同的错误消息。
错误 C1189:#error:WinSock.h 已包含在内
使用带有 boost 1.62 库的 Visual Studio 2010。 是否可以编译和运行这种东西?
添加 C++ 代码的初级部分。
#ifndef _THREADS
#define _THREADS
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#include <string>
#include <queue>
namespace testThreads
{
class boostthreads
{
boost::asio::io_service _io_service;
boost::asio::io_service::work _work;
boost::thread_group _Threads;
std::queue<std::string> _queueOfRequests;
boost::mutex _mutex;
void handleCurrentRequest();
unsigned long getThreadId ( std::string& currentThreadIdA);
public:
boostthreads();
~boostthreads();
};
}
#endif
在帖子评论的讨论中,我们发现发生 C2146 错误,因为您的 C header 中需要 BSTR typedef 定义。 Including of Windows.h before the C header leads to conflict due to double include of WinSock.h by both Windows.h and boost/asio.hpp. 但是 BSTR 是您的库从 Windows.h 中唯一需要的东西,因此解决方案是仅包含定义 BSTR 的 header。 这个 header 是 WTypes.h。 仅供参考,Microsoft 文档上的 BSTR: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.