简体   繁体   English

C++ Boost::thread 调用 c function - 面临编译错误

[英]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. 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.以下是我的 C++ header 文件中包含的头文件列表。

#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和 c 代码 header 开头如下

    #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.我无法控制 C 代码,但可以控制我正在调用的 C++ 代码。

Error message I'm experiencing is as follows我遇到的错误信息如下

error C2146: syntax error: missing ';'错误 C2146:语法错误:缺少“;”

However, if I reverse the order of include files, I get a different error message.但是,如果我颠倒包含文件的顺序,我会收到不同的错误消息。

error C1189: #error: WinSock.h has already been included错误 C1189:#error:WinSock.h 已包含在内

Using visual studio 2010 with boost 1.62 libraries.使用带有 boost 1.62 库的 Visual Studio 2010。 Is it possible to compile and run this sort of stuff?是否可以编译和运行这种东西?

Adding primary section of C++ code.添加 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

During the discussion in the post comments we found that C2146 error occurred because BSTR typedef definition is required in your C header.在帖子评论的讨论中,我们发现发生 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. 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.但是 BSTR 是您的库从 Windows.h 中唯一需要的东西,因此解决方案是仅包含定义 BSTR 的 header。 This header is WTypes.h.这个 header 是 WTypes.h。 Just FYI, BSTR on Microsoft docs: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr仅供参考,Microsoft 文档上的 BSTR: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM