ignoring #pragma comment ws2_32.lib [-Wunknown-pragmas] [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am trying to compile some C++ code using Qt creator that has to connect to a socket for sending and receiving data. I have linked the library file, added the flag win32: LIBS+= -lWS2_32. Attaching the code snippet below:
...
#include<winsock2.h>
#pragma comment(lib,"Ws2_32.lib")
#include<windows.h>
...
WSADATA wsa;
SOCKET s;
uint8_t* encode_buffer;
uint8_t* decode_buffer;
uint16_t encode_buffer_length;
uint16_t decode_buffer_length;
if (send(s, encode_buffer, encode_buffer_length,0)<0){
...
}
if((recv_size=recv(s, decode_buffer, decode_buffer_length,0))== SOCKET_ERROR){
...
}
And my following issues are:
Warning: ignoring #pragma comment [-Wunknown-pragma]
Error: invalid conversion from 'uint8_t* {aka unsigned char*} to 'const char*'[-fpermissive]
Error: invalid conversion from 'uint8_t* {aka unsigned char*} to 'char*'[-fpermissive]
Error: no matching function for call to 'send'
Error: no matching function for call to 'recv'
Warning: unknown pragma ignored
I have added the ws2_32.lib file to the project as an external library, and mentioned it in the #pragma directive.
Not sure where I am going wrong.
Edit: Thanks! Reinterpret_cast solved the issue

Regarding the warnings (not errors), the #pragma you are using is not supported by most compilers (Microsoft and Embarcadero compilers do). So just remove it altogether (you already link to the library in the project makefile), or at least disable it with an appropriate #if/def.
As for the rest of the errors, send() and recv()expect char* pointers, not uint8* pointers. A simple type-cast will suffice.
Try this:
...
#if defined(_MSC_VER) || defined(__BORLANDC__)
#pragma comment(lib,"Ws2_32.lib")
#endif
...
if (send(s, reinterpret_cast<char*>(encode buffer), encode_buffer_length, 0) < 0) {
...
}
if ((recv_size = recv(s, reinterpret_cast<char*>(decode buffer), decode_buffer_length, 0)) == SOCKET_ERROR) {
...
}
...

Related

How to add C library to Visual Studio project [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to add https://github.com/mlabbe/nativefiledialog this C library to my C++ project in visual studio 2022. I've built and generated the .lib file and added it to my project by going to Project->Properties->Linker. I added the path to this .lib file to Additional Library Directories. After doing this, I am still receiving "unresolved external symbol" linker errors. Specifically LNK2019 and LNK1120. I've also added the header file to the Additional Include Directories. What other steps am I missing?
Thanks for the help in advance!
EDIT:
Here is the code implementation:
StartupLayer.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Walnut/Application.h"
#include "ExcelReader.h"
#include "nfd.h"
#define BUFFER_SIZE 255
class StartupLayer : public Walnut::Layer {
private:
ExcelReader reader;
char inputPath[BUFFER_SIZE] = "enter folder";
public:
StartupLayer() : reader(ExcelReader()) {}
~StartupLayer() {}
virtual void OnUIRender() override;
};
StartupLayer.cpp
#include "StartupLayer.h"
void StartupLayer::OnUIRender() {
ImGui::Begin("Select Folder");
ImGui::InputText("select folder", inputPath, BUFFER_SIZE);
// when browse button is clicked
if (ImGui::Button("Browse")) {
nfdchar_t* outpath = NULL;
nfdresult_t result = NFD_OpenDialog(NULL, NULL, &outpath);
}
ImGui::End();
}
It's a Dear ImGui project, and I wanted to use this open file dialog library in one of my layers. The above code is that layer class, and I'm getting the unresolved external symbol on the NFD_OpenDialog function call.
C Code
The C app contains just a little function:
// ./code/c/lib.c
#include <stdio.h>
__declspec(dllexport) void f()
{
printf("\n This is a C code\n");
}
The keyword __declspec(dllexport) is only valid within the Microsoft compiler world and exports that function.
Compile C
Open the Visual Studio Developer Command Prompt and navigate to the folder, where the C project is. Then simply type: cl /LD lib.c. This will create two files. A lib.lib and a lib.dll.
C++ Code
Also nothing special. First you need the header file:
// ./code/cpp/Console/Console/Header.h
#pragma once
extern "C" {
__declspec(dllimport) void f();
}
You find declspec again, but this time it is specified with dllimport which makes perfectly sense as you are importing the function
Our app:
// ./code/cpp/Console/Console/Console.cpp
#include <iostream>
#include "Header.h"
#pragma comment(lib, "../../../c/lib.lib")
int main()
{
f();
}
Note the line #pragma comment(lib, "../../../c/lib.lib") which specifies the location of the .lib so the linker is able to reference the function f().
Execute
If you now execute Console.exe you will get an error because the dll can not be find. To fix this, just copy the lib.dll to the folder where Console.exe is.
Source.
I just forgot to add the path to the generated .lib file (nfd.lib) to Projects->Properties->Linker->Input Additional Dependencies. After doing this, everything worked perfectly.

GLEW giving bunch of uncoused errors [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to learn GLFW and i found a good tutorial online.
I did everything they did but when i even try to write a line of code to main() a lot of errors occur.
The first two error given are
In file included from main.cpp:10:0:
/usr/include/GL/glew.h:15769:109: error: conflicting declaration ‘typedef void (* PFNGLGETFRAGMENTMATERIALIVSGIXPROC)(GLenum, GLenum, const GLint*)’
typedef void (GLAPIENTRY * PFNGLGETFRAGMENTMATERIALIVSGIXPROC) (GLenum face, GLenum pname, const GLint* data);
^
In file included from /usr/include/GL/gl.h:2055:0,
from /usr/include/GLFW/glfw3.h:153,
from main.cpp:7:
/usr/include/GL/glext.h:11616:25: note: previous declaration as ‘typedef void (* PFNGLGETFRAGMENTMATERIALIVSGIXPROC)(GLenum, GLenum, GLint*)’
typedef void (APIENTRYP PFNGLGETFRAGMENTMATERIALIVSGIXPROC) (GLenum face, GLenum pname, GLint *params);
^
Followed by many errors that look like
In file included from main.cpp:10:0:
/usr/include/GL/glew.h:16432:17: error: ‘PFNGLCLIENTACTIVETEXTUREPROC’ does not name a type
GLEW_FUN_EXPORT PFNGLCLIENTACTIVETEXTUREPROC __glewClientActiveTexture;
^
Since there were to many error i will paste them in pastebin:
here
My code is:
#include <iostream>
#include <GLFW/glfw.h>
#include <GL/glew.h>
int main()
{
return 0;
}
I compile it using this command:
g++ -o exec main.cpp -I/usr/include/libdrm -lglfw -I/usr/include/libdrm -lGL -I/usr/include/libdrm -lGLEW -lGLU
I have everything installed and i can create GLFW programs without glew but glew seems to be causing those problems.
Thank you!
EDIT:
I use Ubuntu if it matters.
from http://glew.sourceforge.net/install.html
Also, GLEW will warn you by issuing a preprocessor error in case you
have included gl.h, glext.h, or glATI.h before glew.h.
I suspect that #include <GLFW/glfw.h> internally does one of these things. Try swapping the includes.
GLFW confirms: http://www.glfw.org/docs/3.0/build.html
If you are using an OpenGL extension loading library such as GLEW, the
GLEW header should also be included before* the GLFW one. The GLEW
header defines macros that disable any OpenGL header that the GLFW
header includes and GLEW will work as expected.

It said there's something wrong with the header file, mmsystem.h, i can't use PlaySound() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <iostream>
#include <stdlib.h>
#include <mmsystem.h>
#include <string>
#include <windows.h>
#pragma comment (lib, "winmm.lib")
using namespace std;
int main() {
PlaySound(TEXT("Happy Birthday To You.wav"), NULL, SND_SYNC);
system("pause");
return 0;
}
C:\Program Files (x86)\CodeBlocks\MinGW\include\mmsystem.h|905|error: 'DWORD' does not name a type|
C:\Program Files (x86)\CodeBlocks\MinGW\include\mmsystem.h|906|error: 'UINT' does not name a type|
C:\Program Files (x86)\CodeBlocks\MinGW\include\mmsystem.h|907|error: typedef 'UINT' is initialized (use decltype instead)|
It just came up tons of the errors in the header file like these, I linked to -lwinmm and checked the library, it still pops up.
PS I am using code blocks.
You need to include windows.h before mmsystem.h. windows.h should be first in your includes. mmsystem.h uses types defined in windows.h (including DWORD and UINT).

Socket programming C++ error undefined reference [duplicate]

This question already has answers here:
Visual C++ code not working in Code::Blocks
(2 answers)
Closed 8 years ago.
#pragma comment(lib,"Ws2_32.lib")
#include<sdkddkver.h>
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<WinSock2.h>
#include<windows.h>
#define SCK_VERSION2 0x0202
using namespace std;
int main()
{
long SUCCESSFUL;
WSAData WinSockData;
WORD DLLVERSION;
DLLVERSION=MAKEWORD(2,1);
SUCCESSFUL=WSAStartup(DLLVERSION,&WinSockData);
SOCKADDR_IN ADDRESS;
int AddressSize=sizeof(ADDRESS);
SOCKET sock_LISTEN;
SOCKET sock_CONNECTION;
sock_CONNECTION=socket(AF_INET,SOCK_STREAM,NULL);
ADDRESS.sin_addr.s_addr=inet_addr("127.0.0.1");
ADDRESS.sin_family=AF_INET;
ADDRESS.sin_port=htons(444);
sock_LISTEN=socket(AF_INET,SOCK_STREAM,NULL);
bind(sock_LISTEN,(SOCKADDR *)&ADDRESS,sizeof(ADDRESS));
listen(sock_LISTEN,SOMAXCONN);
for(;;)
{
cout<<"\n\tSERVER:Waiting for incoming connection...";
if(sock_CONNECTION=accept(sock_LISTEN,(SOCKADDR *)&ADDRESS,&AddressSize));
{
cout<<"\n\tA connection was found!"<<endl;
SUCCESSFUL=send(sock_CONNECTION,"Welcome! You are now connected to the Server!",46,NULL);
}
}
}
Whenever I try building this code for server I get following errors:
I'm totally new. I'm using code blocks. I've been searching for solution from last 4 days but I'm not understanding anything. Please help
If you are using CodeBlocks, then you use MinGW that goes with it (I guess, according to low experience level).
This feature works at Visual Studio compilers:
#pragma comment(lib,"Ws2_32.lib")
With MinGW such #pragma will not work.
Instead if it you have to open "Project" -> "Options", then choose your project at left tree (or "Debug"/"Release", if you want that change work just there), and then open tab "link settings".
Press "Add" and find library name like "libws2_32.a" in your MinGW/lib/ directory.
After try rebuild. Hope that will helps.

Deprecated functions not spotted if using "System::Threading::ThreadState" (and others!) C++ VS2005/2008

I'm facing an issue with c++ on vs2005 and also vs2008...
here's how you can reproduce the issue....
create a new (c++) project called 'test' (file|new|project)
select "Windows Forms Application"
and add the 'stdio.h' include and the code fragment below into the test.cpp source file.....
-------------------start of snippet--------------------
#include <stdio.h>
...
int main(array<System::String ^> ^args)
{
int i;
System::Threading::ThreadState state;
char str[20];
sprintf (str, "%s", "test string");
...
-------------------end of snippet--------------------
If you compile the code as above (you'll have to 'buildall' first), you'll get two warnings about 'i' and 'state' being unreferenced (nothing about sprintf being deprecated).
If you comment out "System::Threading :Thread state;", you'll get one warning about 'i' being unreferenced and another warning (C4996) for the 'deprecated' sprintf statement....
This issue also occurs for "System::Windows::Forms::MessageBoxIcon", "System::Base64FormattingOptions" (and perhap all 'enum class' types!)
Anyone know of the cause and workaround to the issue demonstrated here ( i have other files that demonstate this issue..). (I had started a thread on msdn, but then found this site! see link below)
Visual Studio 2005 has stopped warning about deprecated functions
This sounds like an issue you should take to Microsoft support.