首页 > 解决方案 > 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

标签: c++multithreadingvisual-studio-2010boostboost-asio

解决方案


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


推荐阅读