C Programming sockets in windows - winsock2.h linking libraries? - c

I am trying to build some Windows socket code using C. I've started with the following basic code to initialize WinSock:
#include <winsock2.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
WSADATA wsa;
SOCKET s;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
};
When I run the code, I get errors such as:
undefined reference to '__imp_WSAStartup'
A quick google suggests this is to do with needing a library linking - ws2_32?
I was using Cygwin, but apparently this is trying to mimic a POSIX environment in Windows and hence MinGW is much more suited in this instance. Hence I've installed this and updated settings in VSCode to use it.
Now my problem is, I'm not sure how to link the required library. Do I do it within my program by adding some code? Do I do it in some sort of separate terminal?
This is all new to me.

Using Cygwin as an environment to code is not recommenced. (See pros and cons of using Cygwin.) Pick one environment or the other. i.e. run GCC in either Windows or Linux, but not both. Code::Blocks comes bundled with GCC, and has versions that can be used in either Windows or Linux.
And yes, the error message undefined reference to indicates that although the header file is there, the library it refers to is not. It needs to be linked using Ws2_32.lib.
If using the Code::Blocks IDE for example, the instruction for linking a library are here.
Or, from the command line using GCC:
For example, if I have a library named libmine.so in
/home/newhall/lib/ then I'd do the following to link it into my
program:
$ gcc -o myprog myprog.c -L/home/newhall/lib -lmine //Linux
C:\> gcc -o myprog myprog.c -LC:\home\newhall\lib -lmine //Windows
...more on gcc command line here.

The line that links your code to a .exe file needs to include -lws2_32 after the .o or .c file.
So in your case if your program is called myprogram.c you would need to run:
gcc -o myprogram.exe myprogram.c -lws2_32
or if you compile and link in seperate steps:
gcc -c -o myprogram.o myprogram.c
gcc -o myprogram.exe myprogram.o -lws2_32

Related

Couldnot execute the dynamically linked program

Hi I tried doing static library and a shared library with the gnu compiler, here is following code
following is the code for the library
calc_mean.c
double mean(double a, double b){
return (a+b)/2;
}
following is my header file calc_mean.h
double mean(double,double);
Now i started creating static library using following commands
first , calc_mean.c is turned into an object file
gcc -c calc_mean.c -o calc_mean.o
second ,the archiver (ar) is invoked to produce a static
library (named libmean.a) out of the object file calc_mean.o
ar rcs libmean.a calc_mean.o
third, created shared library before that using -fPIC option
created an independant code which is necessary for shared library
gcc -c -fPIC calc_mean.c -o calc_mean.o
now the shared library is created using following command line
gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so.1.0.1 calc_mean.o
finally my main.c file that uses the library is as follows
#include <stdio.h>
#include "calc_mean.h"
int main(int argc, char* argv[]){
double v1,v2,m;
v1 = 5.2;
v2 = 7.9;
m=mean(v1,v2);
printf("The mean of %3.2f and %3.2f is %3.2f\n",v1,v2,m);
return 0;
}
finally I linked the program against static library that generated a statically_linked.exe
gcc -static main.c -L. -lmean -o statically_linked
when dynamically linked, it generated a dynamically_linked.exe with the following command
gcc main.c -o dynamically_linked -L. -lmean
Now when i use the command to execute the dynamically linked program using the following command, Iam getting an error message saying LD_LIBRARY is not recognized as an internal or external command,operable program or batch file
LD_LIBRARY_PATH=.D:\c\project3./dynamically_linked
how can I execute the dynamically linked program?
Your last line suggests that you do all this on windows. But you seem to have followed a step by step guide for linux or another *nix platform.
On windows, a dynamically linked library is in .dll format (not .so) and there's no versioning convention, so the command to create the shared library should look a little different.
Instead of:
gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so.1.0.1 calc_mean.o
do the following on windows:
gcc -shared -Wl,--out-implib,libmean.dll.a -o mean-1.dll calc_mean.o
This creates the library itself named mean-1.dll as well as an import library named libmean.dll.a. The import library (sometimes called .lib instead of .dll.a) on the windows platform is just a stub used during linking of your program, AFAIK MinGW doesn't need it, but other compilers could.
Linking your main program should then work with the same command:
gcc main.c -o dynamically_linked -L. -lmean
And for finding .dll libraries in windows, there is no LD_LIBRARY_PATH -- windows just looks for them in the standard search path, including the directory of the .exe requiring it. IOW, you should be able to just run your program.

Eclipse: unrecognized option '-pthread'

I'm new programming in C, now I'm trying to implement threads in a motor control program I am developing but my problem is that I can not run the pthread.h library in eclipse. despite haver installed POSIX threading library for Win32
Info: Internal Builder is used for build
gcc -O3 -g3 -Wall -pthread -c -fmessage-length=0 -o src\main.o ..\src\main.c
gcc: error: unrecognized option '-pthread'
Info: Parallel threads used: 1
Even though I'm just stating nothing but the library
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main(void) {
return EXIT_SUCCESS;
}
check forum several times and did what they wanted to establish in the -pthread miscellaneous and link phtread but I can not solve the problem, could someone please help me?
I have Windows 10, 64 bits.
If your code is not compiling, you need to add -pthread argument in your gcc compilation as follows.
Navigate to: Project -> Properties
On the left: c/c++ build -> GCC C Compiler -> Miscellaneous
Add -pthread argument at the beginning Other Flags section
Also pthread library add c/c++ build -> Settings -> GCC C Linker -> Libraries
And include pthread library into other libraries.
Click apply.
Clean and build the project.
If you have only Unresolved inclusion problem alone then specify the file system path for the pthread.h as your compiler does not know this.
Check here how to add pthread.h in your filesystem.
I gave up trying, resort to the use of counters card that I use (KL25Z) and events programming.

makefile error: sys/socket.h no such file or directory under Windows

I am using following makefile below:
CC=g++
all: socket.exe
socket.exe: socket.o
g++ socket.o -o socket.exe
socket.o: socket.cpp
g++ -c socket.cpp
When I run make it show error:
socket.cpp: sys/socket.h: no such file or directory.
How to fix it? I am working on Windows.
<sys/socket.h> is for UNIX/Linux.
For windows, you use <Winsock2.h>. You'll also need to link against Ws2_32.lib and call WSAStartup to use WinSock.
See also:
socket function (MSDN)
Windows Socket Programming in C (Stack Overflow)

Setting up SDL2 with MinGW and Sublime Text

I keep getting the error "undefined reference to WInMain#16" when I include SDL2/SDL.h in my C file. It's a simple "Hello" program with the SDL include, and if I remove the SDL include it compiles just fine (as expected).
The problem is I'm new with the compile flags for C (and SDL) and I'm not sure how I link(?) the files together (or if that's necessary). I'm coding using Sublime Text 3 so I'm not sure how you would link SDL as you would when using an IDE.
(D:\CODE\Privata Projekt\C\test.c)
#include <stdio.h>
#include "SDL2/SDL.h"
int main(int argc, char *argv[]) {
printf("hello\n");
return 0;
}
My paths to MinGW and SDL2 is:
C:\MinGW\include\SDL2 (all my sdl header files reside in here too)
C:\MinGW\include\SDL2\bin
C:\MinGW\include\SDL2\lib
C:\MinGW\include\SDL2\share
And I build the program with
gcc test.c -o test
EDIT:
What worked for me was to use these flags, in this exact same order
-lmingw32 -LC:\MinGW\include\SDL2\lib -lSDL2main -lSDL2
You need to link with the library as well. You can do it by passing the correct options on the command line: -L to tell the linker where to find the library, and -l (lowercase L) to tell the linker to link to the library.
Like
> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -lSDL2
(I don't know the name of the library, so change SDL2 to the appropriate name.)
If there is problem running your program due to the loader not finding the SDL2 library, you may have to add another option which tells linker the place of the dynamic library:
> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -Wl,-rpath=C:\MinGW\include\SDL2\lib -lSDL2
I don't know if it's needed or even used on Windows though. You might have to copy the DLL to the directory where the executable is.

How do I add a directory to C header include path?

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c

Resources