How to compile program with windows sockets in linux? - c

I have a program written in C which uses the following libraries:
#ifdef _WIN32
#include <winsock2.h>
#define socklen_t int
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
In Windows, in order to compile this program with gcc, you had to use -lWs2_32 property.
How to compile it using gcc in linux?

-lWs2_32 is a linker option to specify a library to link. The compiler does not use it.
For gcc under Linux there is no need to explicitly specify a library when using socket functions.

I think you should statically link the executable and use MinGW. But it's just an assumption. Never tried it.

-lWs2_32 is a linker option to specify a library to link. The compiler does not use it.
For gcc under Linux there is no need to explicitly specify a library when using socket functions.

Related

gcc tdm 10.3.0 get missing header files

I downloaded tdm gcc 10.3.0 and installed it succesfully. I have a file I want to compile yet it is missing header files. Is there a way to add components to tdm gcc from an interface, or do I download the files(headers) manually and add them to the project. If so, where do I add them in the project ? I know header files exist in C:\TDM-GCC-32\include
Also, just in case, I have these header files in my main.c,
#include <winsock2.h>
#include <Windows.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <bcrypt.h>
#include <wincrypt.h>
#include <sysinfoapi.h>
#include <winternl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
as i tried to compile with gcc main.c, I got this error
main.c:8:10: fatal error: ws2ipdef.h: No such file or directory
8 | #include <ws2ipdef.h>
| ^~~~~~~~~~~~
compilation terminated.
How can I find this header file? If I just find it in github is it enough to add it to C:\TDM-GCC-32\include ?
another way to ask, how can I add ws2 to tdm gcc, or how to install tdm gcc alongwith?

To compile intel intrinsic and reverse assemble but not working [duplicate]

I'm trying to run a Visual Studio cpp project created by a friend of mine. I'm trying to run the file without VS. But I'm getting a list of errors, all in the same format:
inlining failed in call to always_inline '__m256d _mm256_broadcast_sd(const double*)': target specific option mismatch|
It runs correctly in VS with release mode and breaks when run in debug mode.
The includes are as follows:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
# include <omp.h>
#include <chrono>
#include <fstream>
#include <algorithm>
#include <immintrin.h>
using namespace std::chrono;
using namespace std;
and the error is called from here:
double zero = 0;
__m256d acc = _mm256_broadcast_sd(&zero);
Update:
I'm using the this command to run it: g++ -std=c++0x multip.cpp -o multip, is there an additional parameter to add -mavx to the compiler invocation?
"Target specific option mismatch" means that you're missing a feature flag from your GCC invocation. You probably need to add -mavx to your compiler invocation.
If you're intending to run this on your computer only, -march=native will turn on all the feature flags that your own machine supports.

unknown type name "pthread_barrier_t"

I am trying to parallelize an algorithm in C. I want to use pthread_barrier_t but my Ubuntu wsl can't find it for some reason. I have pthread.h included and I can use the rest of the pthread functions. libthread.a is installed.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Error occurs here */
pthread_barrier_t barrier;
Exact error is: "identifier pthread_barrier_t is undefined"
I saw elsewhere it could be the way I'm compiling.
Compiling as follows:
gcc -o test test.c -Wall -std=c99 -lpthread -lm
Also, VS Code can't identify the function.
The problem is your -std=c99 option. Using strict C mode disables a bunch of stuff, including something that stops pthread_barrier_t from getting defined. If you use -std=gnu99 instead, it should compile. (Tested on Ubuntu 16.04 on WSL).
Alternatively, add
#define _XOPEN_SOURCE 600 /* Or higher */
or
#define _POSIX_C_SOURCE 200112L /* Or higher */
before the first #include in your source. See man 7 feature_test_macros for the acceptable values of these macros and more information.

F_SEAL_SEAL undeclared, even when headers are included

I'm trying to use file sealing on Linux. Here's an example C program.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
int main(void) {
(void)F_SEAL_SEAL;
}
You can build it using gcc -Wall -o ./linux_file_sealing linux_file_sealing.c or similar.
When I build it, I get an error about F_SEAL_SEAL.
gcc -Wall -o ./linux_file_sealing linux_file_sealing.c
linux_file_sealing.c: In function ‘main’:
linux_file_sealing.c:7:19: error: ‘F_SEAL_SEAL’ undeclared (first use in this function)
printf("%d\n",F_SEAL_SEAL);
^
linux_file_sealing.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
I'm including unistd.h and fcntl.h, as per the man page... so what else should I be doing, and where is that described?
(The man pages just say that sealing is "Linux-specific", but give no further details. This is the reason for including the GNU_SOURCE define, which is how you get the other Linux-specific stuff, but for F_SEAL_SEAL it seems to make no difference.)
(Ubuntu 16.04 LTS, Linux 4.4.0-36)
You want
#include <linux/fcntl.h>
instead of
#include <fcntl.h>

warning: implicit declaration of function 'kill'

I am making these inclusions:
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
But still getting this warning.
You are probably passing the "-ansi -Wall" switches to the gcc compiler.
You can remove "-ansi" if you don't need it, otherwise try to set the proper feature define macro.
Something like:
#define _POSIX_SOURCE
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
You can also define it on the compile time by adding this flag with gcc.
-D_POSIX_C_SOURCE
ex:
-g -D_POSIX_C_SOURCE -Wall -std=c99
According to the kill(2) Linux manual page, _POSIX_C_SOURCE must be #defined before any includes in order for the prototype of the function kill() to be included through <signal.h> if you are using glibc.
Your code will probably compile either way, since the binary is available for linking regardless of the defines, but that's the proper way to silence this warning according to the documentation.

Resources