I'm trying to complie and run my C program. The program uses threads. I'm running windows 10 using WSL with Ubuntu terminal. (Also trying it with Ubuntu virtual box) This is my "default" Makefile format im using for all my programs (changing name and flags for each)
CC=gcc
CFLAGS=-I. -w -pthread
DEPS = v1.h
version1: v1
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
v1: v1.o
$(CC) -o v1 v1.o
This is the first time im using threads in C which led me to discover -pthread. I found out you need to add it to the flags (which i did with CFLAGS). For some reason when i run this makefile above it got errors not finding pthread functions and i notice the way to fix it was by changing this line:
$(CC) -o v1 v1.o -pthread
adding pthread at the end. All this led me to make some research about flags in general and after searcing gcc's man page and google I found no simple answer for these questions:
Thank you.
Why i needed to add -pthread to the .o task and .c task?
Because you use functions from posix threads library.
Why it's not "enough" adding it to just one?
Because both the compiler (that what compiles .c
into .o
) and linker (it links multiple .o
into an executable) need to know about these functions. The compiler checks if the functions exists in the libraries and the linker connects the proper functions that you used in your code to symbols in the library.
What is -w flag?
From man gcc:
-w Inhibit all warning messages.
Please do not use -w
. The usual is to use -Wall -Wextra
to enable all possible warnings and catch as many problems at compile time.
I know it stands for "Warnings" but what is the diffrent between -w and -Wall?
-w
disables warnings, -Wall
enables specified list of warnings.
What is -I. flag?
From man gcc
:
-I dir
Add the directory dir to the list of directories to be searched for header files during preprocessing.
It adds the directory to the search path of the preprocessor. One of the jobs of preprocessos is finding where #include <this files are>
. The files in #include
statements are searched in "preprocessor search paths". -I
adds the path to this list.
Is it LDLAGS or LDFLAGS?
From makefile manual it's LDFLAGS
....
What does LDFLAGS stand for?
ld flags.
Side note: This is my "default" Makefile format im using for all my programs
make
is an old grandpa - it's over 40 years old. I would kindly suggest to learn a build system that is easier to handle in todays world, like cmake
.