Why can't I import header files to a library i

2019-07-26 07:01发布

I'm using the C++ socket.io library in my C++ project, and I'm unable to include the required header files in my projects header file.

But when I include the required header files in my CPP file, I am successfully able to include the required header files.

Steps taken to utilize the library:

  1. Compile the C++ websocketpp, openssl, boost library, and prepare the JSON library.
  2. Link against the *.lib files in my VS2015 project.
  3. Include the header files location in the C++ additional include dependencies folder.
  4. Include the *.lib files location in the Linker include dependencies folder.
  5. Include the three header files in my CPP file.
  6. Build. Yay! It works!
  7. Attempt to encapsulate the libraries functionality into my own class/object.
  8. Attempt to include the three libraries header files in my header file. Notice a plethora of compilation errors about initializing.

Header File Import

#include <sio_message.h>
#include <sio_socket.h>
#include <sio_client.h>

Error when including header files within header file:

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   'initializing': cannot convert from 'nullptr' to 'const int &'  c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2838   'function<void __cdecl(sio::message::list const &)>': illegal qualified name in member declaration  c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2838   'function<void __cdecl(sio::message::list const &)>': illegal qualified name in member declaration  c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2440   'initializing': cannot convert from 'nullptr' to 'const int &'  c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2838   'list': illegal qualified name in member declaration    c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2838   'list': illegal qualified name in member declaration    c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2439   'sio::socket::ack': member could not be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2439   'sio::socket::ack': member could not be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2439   'sio::socket::msglist': member could not be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2439   'sio::socket::msglist': member could not be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2530   'sio::socket::name': references must be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 96  
Error   C2530   'sio::socket::name': references must be initialized     c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 96  
Error   C2838   'string': illegal qualified name in member declaration      c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2838   'string': illegal qualified name in member declaration      c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2059   syntax error: ')'       c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2059   syntax error: ')'       c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2143   syntax error: missing ')' before 'const'        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2143   syntax error: missing ')' before 'const'        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2143   syntax error: missing ';' before 'const'        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  
Error   C2143   syntax error: missing ';' before 'const'        c:\root\src\socket.io-client-cpp\build\include\sio_socket.h 75  

Possible Problems:

  1. Shared Namespace? Maybe I'm propagating an incorrect namespace?
  2. Circular Dependencies? Maybe I'm causing a circular dependency by including the header files in my class, and one of the other header files are including that same file.

标签: c++ socket.io
1条回答
小情绪 Triste *
2楼-- · 2019-07-26 07:42

Solution:

I figured out what the problem happened to be. Both the Socket.io C++ library and Qt share namespace in regards to the word "emit". In the event that somebody else runs into this problem here is how I resolved it:

  1. git clone https://github.com/raksa/SocketIO-cpp-lib
  2. Edit all instances of the term "emit" in the source files. In my case I changed them all to "emit_signal".
  3. git checkout development within the websocketpp library. (Wouldn't compile on master or from the most recent tag as of 2/16/2017).
  4. Recompile the sioclient.lib file from the SocketIO-cpp-lib project for both debug and release.
  5. Go to your project and link against the lib file. Also, provide the lib file path.
  6. Include the sioclient.h, siosocket.h, and siomessage.h file's directory to your include path.
  7. Include both the boost libraries path and boost libraries *.lib files to your project.
  8. You shouldn't have errors when including any of the Socket.io C++ header files in your project.

Hope this helps anybody else with the same issue.

Note: I also ran into another problem when building the sioclient.lib file. You might need to make an "optimized" and "debug" folder in one of the project folders. The CMAKE script incorrectly references folders that don't exist in the project. That, or delete those lines from the CMAKE script.

查看更多
登录 后发表回答