c++ - 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.
Following is the list of headers included in my C++ header file.
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#include <string>
#include <queue>
and c code header starts as follows
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MS_WINNT) || defined(WIN32)
#include <windows.h>
typedef BSTR ARG;
I don't have a control over the C code but have a control over C++ code that I'm invoking.
Error message I'm experiencing is as follows
error C2146: syntax error : missing ';'
However, if I reverse the order of include files, I get a different error message.
error C1189: #error : WinSock.h has already been included
Using visual studio 2010 with boost 1.62 libraries. Is it possible to compile and run this sort of stuff?
Adding primary section of C++ code.
#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
解决方案
During the discussion in the post comments we found that C2146 error occurred because BSTR typedef definition is required in your C header. 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. However BSTR is the only thing your library needs from Windows.h, so the solution is to include just the header where BSTR is defined. This header is WTypes.h. Just FYI, BSTR on Microsoft docs: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
推荐阅读
- postgresql - Citus PostgresQL 需要注意哪些设置
- citus - 将数据同步到插入时离线的节点(Citus)
- ssl - FreeRTOS+TCP 和 mbedTLS+TCP
- python - Python Netmiko,我可以将我的脚本指向一个包含 IP 列表的文本文件吗?
- python - 如何用条件逐行替换
- python-3.x - 如何改进锥体 - 使底座更像圆形?
- electron - Electron Project 无法使用正确的路径归档
- c# - 如何在 Xamarin.forms C# 中将标签放置在相对于图像的特定位置?
- algorithm - 我如何证明以下算法?
- apache-flink - 如何在 Flink CEP 中指定状态应该保持的时间