gcc编译和gcc -lm -Wall -o之间的区别?

时间:2021-06-06 02:12:20

I am working on a homework assignment for my CS class and we are required to develop this simple program for our first use of C. I was able to create the program through a gcc compile but while using gcc -lm -Wall -o compile, my program crashes and returns

我正在为我的CS类做一个家庭作业,我们需要为我们第一次使用C开发这个简单的程序。我能够通过gcc编译创建程序但是在使用gcc -lm -Wall -o compile时,我的程序崩溃并返回

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What would cause my program to crash when entering it into the second compile?

什么会导致我的程序在进入第二次编译时崩溃?

2 个解决方案

#1


2  

To compile your code with gcc, you also need to pass in the names of your source files. So if you had a source file named project1.c, you could compile it by running:

要使用gcc编译代码,还需要传入源文件的名称。因此,如果您有一个名为project1.c的源文件,则可以通过运行以下命令对其进行编译:

gcc -lm -Wall -o a.out project1.c

Assuming your code successfully compiled (no errors), you could run it by then doing:

假设您的代码已成功编译(无错误),您可以通过执行以下操作来运行它:

./a.out

That being said, your code won't compile. You can't use the modulus operator against a floating point number.

话虽这么说,你的代码将无法编译。您不能对浮点数使用模数运算符。

foo.c:9:25: error: invalid operands to binary expression ('float' and 'float')
    leftover= enrollment%25;
              ~~~~~~~~~~^~~

#2


1  

To learn about the command line arguments for gcc, you can use

要了解gcc的命令行参数,可以使用

$ man gcc

The documentation is very daunting because there are a lot of options. Some experience with man pages will help you quickly scan for the information you need.

文档非常令人生畏,因为有很多选项。手册页的一些经验将帮助您快速扫描所需的信息。

The first thing to look at is the SYNOPSIS section at the top. This shows the usage of gcc. Anything in [] is optional. Note at the very end of this section, there is infile.... This means that you must provide at least one file name for the compiler to process. I suspect you are getting an error because you are missing this.

首先要看的是顶部的SYNOPSIS部分。这显示了gcc的用法。 []中的任何内容都是可选的。注意在本节的最后,有infile ....这意味着您必须至少提供一个文件名供编译器处理。我怀疑你错了,因为你错过了这个。

So the correct command line should be

所以正确的命令行应该是

$ gcc -Wall -o <program> <program>.c -lm

This will compile your .c file to an executable with the same name without an extension.

这会将.c文件编译为具有相同名称但没有扩展名的可执行文件。

For details about the options you are using, the following comes directly from the gcc man page.

有关您正在使用的选项的详细信息,以下内容直接来自gcc手册页。

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

   -llibrary
   -l library
       Search the library named library when linking.

   -o file
       Write output to file.  

The errors you get are due to the -Wall option.

您获得的错误是由-Wall选项引起的。

The -lm flag links in a library named "m". This is a math library which you probably don't need to worry about for now. For more details about this library use man libm.

-lm标志链接在名为“m”的库中。这是一个数学库,您现在可能不需要担心。有关此库的更多详细信息,请使用man libm。

Note that the -o option requires an argument. In your case, this tells gcc the name of the executable to create. (This is the reason for -o <program> in my suggested solution above.)

请注意,-o选项需要参数。在您的情况下,这告诉gcc要创建的可执行文件的名称。 (这是上面我建议的解决方案中-o 的原因。)

#1


2  

To compile your code with gcc, you also need to pass in the names of your source files. So if you had a source file named project1.c, you could compile it by running:

要使用gcc编译代码,还需要传入源文件的名称。因此,如果您有一个名为project1.c的源文件,则可以通过运行以下命令对其进行编译:

gcc -lm -Wall -o a.out project1.c

Assuming your code successfully compiled (no errors), you could run it by then doing:

假设您的代码已成功编译(无错误),您可以通过执行以下操作来运行它:

./a.out

That being said, your code won't compile. You can't use the modulus operator against a floating point number.

话虽这么说,你的代码将无法编译。您不能对浮点数使用模数运算符。

foo.c:9:25: error: invalid operands to binary expression ('float' and 'float')
    leftover= enrollment%25;
              ~~~~~~~~~~^~~

#2


1  

To learn about the command line arguments for gcc, you can use

要了解gcc的命令行参数,可以使用

$ man gcc

The documentation is very daunting because there are a lot of options. Some experience with man pages will help you quickly scan for the information you need.

文档非常令人生畏,因为有很多选项。手册页的一些经验将帮助您快速扫描所需的信息。

The first thing to look at is the SYNOPSIS section at the top. This shows the usage of gcc. Anything in [] is optional. Note at the very end of this section, there is infile.... This means that you must provide at least one file name for the compiler to process. I suspect you are getting an error because you are missing this.

首先要看的是顶部的SYNOPSIS部分。这显示了gcc的用法。 []中的任何内容都是可选的。注意在本节的最后,有infile ....这意味着您必须至少提供一个文件名供编译器处理。我怀疑你错了,因为你错过了这个。

So the correct command line should be

所以正确的命令行应该是

$ gcc -Wall -o <program> <program>.c -lm

This will compile your .c file to an executable with the same name without an extension.

这会将.c文件编译为具有相同名称但没有扩展名的可执行文件。

For details about the options you are using, the following comes directly from the gcc man page.

有关您正在使用的选项的详细信息,以下内容直接来自gcc手册页。

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

   -llibrary
   -l library
       Search the library named library when linking.

   -o file
       Write output to file.  

The errors you get are due to the -Wall option.

您获得的错误是由-Wall选项引起的。

The -lm flag links in a library named "m". This is a math library which you probably don't need to worry about for now. For more details about this library use man libm.

-lm标志链接在名为“m”的库中。这是一个数学库,您现在可能不需要担心。有关此库的更多详细信息,请使用man libm。

Note that the -o option requires an argument. In your case, this tells gcc the name of the executable to create. (This is the reason for -o <program> in my suggested solution above.)

请注意,-o选项需要参数。在您的情况下,这告诉gcc要创建的可执行文件的名称。 (这是上面我建议的解决方案中-o 的原因。)