-1

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:

  1. Why i needed to add -pthread to the .o task and .c task? Why it's not "enough" adding it to just one?
  2. What is -w flag? I know it stands for "Warnings" but what is the diffrent between -w and -Wall?
  3. What is -I. flag? Again, I found it stands for "include" but I'm not sure what its doing.. My makefile work with or without that flag.

Thank you.

Gal Birka
  • 581
  • 6
  • 16
  • Doesn't C have native threads by now? Since C11 ? – MSalters May 25 '20 at 10:24
  • @MSalters My gcc is version 7.4.0. How do i check/change my C version? – Gal Birka May 25 '20 at 10:31
  • 1
    @Birkagal: See https://stackoverflow.com/questions/16256586/how-to-enable-c11-on-later-versions-of-gcc – MSalters May 25 '20 at 10:35
  • 3
    Note, that this is a forum for one specific question per question, not multiple ones. I believe this forum also much appreciates the work that askers give into finding the answer to their questions - I believe some your question could be easily found in the internet and in your compiler documentation. [On-topic](https://stackoverflow.com/help/on-topic) and [how-to-ask](https://stackoverflow.com/help/how-to-ask) are helpful resources. – KamilCuk May 25 '20 at 10:58
  • 1
    Agree with @KamilCuk. OP, flag related questions are easy to find, please take your time next time. First question is somehwat valid though, since it might need some elaboration for people dealing with this for the first time. – Andrejs Cainikovs May 25 '20 at 11:08

2 Answers2

1

1: Why i needed to add -pthread to the .o task and .c task? Why it's not "enough" adding it to just one?

As per man gcc:

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

You need to specify -pthread option both to compiler and linker part of your Makefile:

CC=gcc
CFLAGS=-I. -w -pthread
LDFLAGS=-pthread
DEPS = v1.h
version1: v1

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

v1: v1.o
    $(CC) -o v1 v1.o $(LDFLAGS)

2: What is -w flag? I know it stands for "Warnings" but what is the diffrent between -w and -Wall?

Check warning options info of gcc.

-w
    Inhibit all warning messages.
-Wall
    This enables all the warnings about constructions that some users consider
    questionable, and that are easy to avoid (or modify to prevent the
    warning), even in conjunction with macros. This also enables some
    language-specific warnings described in C++ Dialect Options and
    Objective-C and Objective-C++ Dialect Options. 
-Wextra
    This enables some extra warning flags that are not enabled by -Wall.
    (This option used to be called -W. The older name is still supported,
    but the newer name is more descriptive.) 

Basically, -w disables all warnings. Use this with caution. I'd suggest you to use -Wall instead.

3: What is -I. flag?

Check options for directory search info of gcc.

-I dir
-iquote dir
-isystem dir
-idirafter dir
    Add the directory dir to the list of directories to be searched for
    header files during preprocessing.

-I. option in your Makefile instructs compiler to look for header files in the directory of Makefile as well. . in Linux world stands for current directory.

Community
  • 1
  • 1
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • THANK YOU.Is it LDLAGS or LDFLAGS? What does LDLAGS stand for? I didn't know I can specify different flags for compiling and linking, now it makes sense. – Gal Birka May 25 '20 at 10:45
  • @Birkagal Fixed typo. – Andrejs Cainikovs May 25 '20 at 10:52
  • Thank you very much friend that really helped me. I'm sorry for asking some "silly questions" I did try look in gcc man page but searched "/-w" instand of "/w " (notince the space) which led to a lot of "not what I was looking results". But I really do appriciate you'r answer. One last question about -I., There is no point for it in this contex right? The complier automatically knows to search for headers in "this" (.) folder right? Thanks again for the answer friend! – Gal Birka May 25 '20 at 11:13
  • 1
    Honestly, I'm not sure about `-I.`, but I assume compiler will look for headers in current directory without this option as well. You're welcome! – Andrejs Cainikovs May 26 '20 at 10:57
1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Thanks for the elaborate answer. I know some of those questions were "silly" but I really tried looking for the answer first on gcc man page but since i searched "/-w" instand of "/-w " (noting the space) i found a lot of not what i was looking for answers. So again thanks for this great answer it really clear up some things in my head. And one last thing about the side node, Makefile is the request from my teacher but I will definitely try and look up cmake for my own knowledge. – Gal Birka May 25 '20 at 11:16
  • 1
    Yea, sure, google sucks when searching a keyword with a leading minus - it interprets it as "exclude" condition. I usually just search without the minus, like `gcc "Wall"` sometimes works. – KamilCuk May 25 '20 at 11:19