几个基本的 GCC 标志问题

Few basic GCC flag questions

我正在尝试编译 运行 我的 C 程序。该程序使用线程。 我正在 运行ning windows 10 使用 WSL 和 Ubuntu 终端。 (也尝试使用 Ubuntu 虚拟框) 这是我用于所有程序的 "default" Makefile 格式(为每个程序更改名称和标志)

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

这是我第一次在 C 中使用线程,这让我发现了 -pthread。我发现你需要将它添加到标志中(我用 CFLAGS 做了)。出于某种原因,当我 运行 上面的这个 makefile 出现找不到 pthread 函数的错误时,我注意到修复它的方法是更改​​此行:

$(CC) -o v1 v1.o -pthread

最后添加 pthread。 所有这些导致我对一般标志进行了一些研究,在搜索 gcc 的手册页和 google 之后,我没有找到这些问题的简单答案:

  1. 为什么我需要将 -pthread 添加到 .o 任务和 .c 任务?为什么不 "enough" 将它添加到一个?
  2. 什么是 -w 标志?我知道它代表 "Warnings" 但 -w 和 -Wall 有什么区别?
  3. 什么是-I。旗帜?同样,我发现它代表 "include",但我不确定它在做什么。我的 makefile 可以使用或不使用该标志。

谢谢。

1: 为什么我需要将-pthread 添加到.o 任务和.c 任务?为什么只加一个还不够?

根据man gcc

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

您需要为 Makefile 的编译器和链接器部分指定 -pthread 选项:

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:什么是-w标志?我知道它代表“警告”,但 -w 和 -Wall 之间有什么区别?

查看 gccwarning options 信息。

-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.) 

基本上,-w 禁用所有警告。请谨慎使用。我建议您改为使用 -Wall

3:什么是-I。旗帜?

查看 gccoptions for directory search 信息。

-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.
Makefile 中的

-I. 选项指示编译器也在 Makefile 目录中查找头文件。 Linux 世界中的 . 代表 当前目录 .

Why i needed to add -pthread to the .o task and .c task?

因为您使用了 posix 线程库中的函数。


Why it's not "enough" adding it to just one?

因为编译器(将 .c 编译成 .o)和链接器(将多个 .o 链接成可执行文件)都需要了解这些函数。编译器检查函数是否存在于库中,链接器将您在代码中使用的正确函数连接到库中的符号。


What is -w flag?

来自 man gcc:

-w Inhibit all warning messages.

请不要使用-w。通常是使用 -Wall -Wextra 启用所有可能的警告并在编译时捕获尽可能多的问题。


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

-w 禁用警告,-Wall 启用 specified list of warnings.


What is -I. flag?

来自man gcc

-I dir

Add the directory dir to the list of directories to be searched for header files during preprocessing.

它将目录添加到预处理器的搜索路径中。预处理程序的一项工作是找到 #include <this files are> 的位置。 #include 语句中的文件在 "preprocessor search paths" 中搜索。 -I 将路径添加到此列表。


Is it LDLAGS or LDFLAGS?

makefile manual开始是LDFLAGS...

What does LDFLAGS stand for?

ld 标志。


旁注:This is my "default" Makefile format im using for all my programs make 是一位老爷爷 - 已经 40 多岁了。我建议学习一个在当今世界更容易处理的 build system,例如 cmake.