2

I am working on a homework assignment for my computer science 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 straight gcc compile, but while using gcc -lm -Wall -o compile, my program crashes and returns

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?

My code

#include <stdio.h>

void main() {
    float enrollment, fullsec, leftover, receiveA, receiveB, receiveC, receiveD;
    printf("Program #1, masc0865, Tom Bachar");
    printf("Enter enrollments on one line: ");
    scanf("%f", &enrollment);
    fullsec = enrollment/25;
    leftover = enrollment%25;
    printf("Enrollment = %f", enrollment);
    printf("Amount of Full Sections = %f", fullsec);
    printf("Students Left Over = %0.2f", leftover);
    receiveA = enrollment*0.30;
    receiveB = enrollment*0.25;
    receiveC = enrollment*0.15;
    receiveD = enrollment*0.30;
    printf("Students expected to receive an A: %f", receiveA);
    printf("Students expected to receive a B: %f", receiveB);
    printf("Students expected to receive a C: %f", receiveC);
    printf("Students expected to receive some other grade: %f", receiveD);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • the "gcc -lm -Wall -o" command is the compile that the teacher will use to compile our program as documented in our handout. – Sherman Bachar Sep 14 '14 at 04:08
  • 1
    Please show the code which you are compiling. – Code-Apprentice Sep 14 '14 at 04:10
  • @marscher: `-lm` is using the `-l` flag in gcc, and is passing it the `m` argument. Together this means to link against `libm.so`, otherwise known as the math library. – Bill Lynch Sep 14 '14 at 04:13
  • @sharth: try invoking gcc -lm without any further arguments – marscher Sep 14 '14 at 04:14
  • @marscher: I understand that if Sherman didn't pass any source files, that this is a reasonable output. But `-lm` is a valid argument to gcc. And it's documented. – Bill Lynch Sep 14 '14 at 04:16
  • @sharth all i type in is gcc -lm -Wall -o ./a.out – Sherman Bachar Sep 14 '14 at 04:18
  • @marscher I tried just passing in gcc -lm and it returned "ld: can't link with a main executable file './a.out' for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" – Sherman Bachar Sep 14 '14 at 04:19
  • 2
    @ShermanBachar You still aren't sending it the actual *source file*, though. For example, if your code is in `homework.c`, use `gcc -lm -Wall -o a.out homework.c` (I think that's the right order). – Chris Hayes Sep 14 '14 at 04:23
  • @Chris Hayes So what I do is go into the file where my code is (classgrades.c) and I compile it with the code gcc -lm -Wall -o homework.c but I get the same return and the file disappears! – Sherman Bachar Sep 14 '14 at 04:26
  • @ShermanBachar: __No!__ If your source file is `classgrades.c`, and you'd like it to produce an executable named `a.out`, then you would do: `gcc -lm -Wall -o a.out classgrades.c`. The argument after `-o` will be __overwritten__. – Bill Lynch Sep 14 '14 at 04:27
  • Change `void main()` to `int main()` and add `return 0;` as the last statement; it won't solve the error but it will reduce a warning. – Ja͢ck Sep 14 '14 at 04:33
  • `leftover= (int)enrollment%25;` should fix the error. – Ja͢ck Sep 14 '14 at 04:35
  • @ShermanBachar You need **both** the output file name and the input file. See my answer for details (or the comments and the other answer). – Code-Apprentice Sep 14 '14 at 04:36
  • 1
    See [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336). – Jonathan Leffler Sep 14 '14 at 04:42
  • For `-lm`, see *[Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)* (18 answers. 314 upvotes. 2009.) – Peter Mortensen Oct 27 '22 at 23:45
  • But it says "`clang:`". Is the executable `gcc` aliased to the [Clang](https://en.wikipedia.org/wiki/Clang) compiler executable (***not*** [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection))? That [wouldn't be the first time](https://stackoverflow.com/questions/38840601/how-can-i-ignore-an-error-when-using-gcc-compile-option-werror#comment131019685_38840601).... – Peter Mortensen Oct 29 '22 at 21:00
  • What common environment or tool aliases executable `gcc` to the Clang compiler? – Peter Mortensen Oct 29 '22 at 21:03
  • Note: the code was added in revision 3, but unexplained removed in revision 7. – Peter Mortensen Oct 29 '22 at 21:15
  • For leaving out the source file (as correctly identified in [Bill Lynch's answer](https://stackoverflow.com/questions/25829950/difference-between-a-straight-gcc-compile-and-gcc-lm-wall-o/25830031#25830031)), I can't reproduce the first lines, but using the Clang 6.0.0 compiler on Linux gives the exact same last line: *"clang: error: linker command failed with exit code 1 (use -v to see invocation)"*. Whereas using a (real) GCC compiler/frontend (7.5.0) gives a very different result: *"collect2: error: ld returned 1 exit status"* – Peter Mortensen Oct 29 '22 at 21:28
  • Thus, I think we can conclude the OP (effectively) used some version of the Clang compiler (but with the frontend/executable name `gcc`), not GCC. – Peter Mortensen Oct 29 '22 at 21:30
  • (Why do compilers have to be so cryptic? Couldn't they just report *"Missing input file(s)."*) – Peter Mortensen Oct 29 '22 at 21:37
  • Conclusion: This is for [Clang](https://en.wikipedia.org/wiki/Clang), ***not*** [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) ("`clang: error: linker command failed with exit code 1`"). This is likely on a Mac [where executable 'gcc' is aliased to the Clang compiler](https://www.quora.com/Why-does-the-gcc-command-on-MacOS-execute-clang). The underlying reason [may be GPLv3](https://www.quora.com/Is-Apple-supporting-Clang-in-order-to-destroy-GCC/answer/Mario-Ray-Mahardhika-1). – Peter Mortensen Nov 08 '22 at 20:23

2 Answers2

4

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 -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;
              ~~~~~~~~~~^~~
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Thank you! How can i fix this float issue? – Sherman Bachar Sep 14 '14 at 04:33
  • @ShermanBachar: It's not clear what you are trying to accomplish, which makes it hard to suggest a fix. – Bill Lynch Sep 14 '14 at 04:34
  • @ShermanBachar Is there a reason you are using `float` rather than `int` or `double`? The most obvious fix seems to be using `int` where appropriate. – Code-Apprentice Sep 14 '14 at 04:42
  • 1
    Libraries after object files, at least for reliable linking. – Jonathan Leffler Sep 14 '14 at 04:43
  • It doesn't change the fix, but [the OP used the Clang compiler, not GCC](https://stackoverflow.com/questions/25829950/difference-between-a-straight-gcc-compile-and-gcc-lm-wall-o/25830031#comment131087032_25829950). – Peter Mortensen Oct 29 '22 at 21:47
  • `a.out` is the default for `gcc` and `clang` when you do not specify the output file; this could be simplified by omitting the `-o a.out`, but usually you would specify the target output. – Neil Apr 29 '23 at 01:19
1

To learn about the command line arguments for gcc, you can use the man page for 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.

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.

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

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

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.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • thanks for the awesome in depth answer, I edited my initial post to include the code I am working with. – Sherman Bachar Sep 14 '14 at 04:21
  • @ShermanBachar I have made an edit to give you a suggested solution to your problem. – Code-Apprentice Sep 14 '14 at 04:29
  • The errors aren't caused by `-Wall`, that's only if you compile with `-Werror`. – Ja͢ck Sep 14 '14 at 04:32
  • @Sherman - looking at your code I see no immediate error. However, as Code-Apprentice suggests you invocation of the compiler seems to be in error. I would be interested in seeing the output of you trying the command he suggests. – thurizas Sep 14 '14 at 04:32
  • Libraries after object files — at least for reliable linking. – Jonathan Leffler Sep 14 '14 at 04:43
  • @JonathanLeffler In this case, I assume you mean "libraries after **source** files", correct? Since we are compiling and linking in a single step, the object files are not given explicitly on the command-line, of course. – Code-Apprentice Sep 14 '14 at 04:47
  • Well, yes, and no, but mainly yes. Yes, put the `-lm` after the source files in the C compiler command line; that gets translated by the compiler into libraries after the object files in the linker command line. – Jonathan Leffler Sep 14 '14 at 04:48
  • [It ***isn't*** GCC, but Clang](https://stackoverflow.com/questions/25829950/difference-between-a-straight-gcc-compile-and-gcc-lm-wall-o?noredirect=1&lq=1#comment131286752_25829950). The name of the executable is `gcc`, but it is on a Mac with executable `gcc` aliased to the [Clang](https://en.wikipedia.org/wiki/Clang) compiler... There are [many more](https://stackoverflow.com/questions/38840601/how-can-i-ignore-an-e#comment131284665_38840601) [of these](https://stackoverflow.com/questions/20296282/compiling-a-c#comment131285788_20296282) kind of questions (most mistagged as GCC). – Peter Mortensen Nov 08 '22 at 20:30
  • @PeterMortensen The OP does not mention which OS they are on, so I think it is not valid to make any assumptions about what is aliased or not. – Code-Apprentice Nov 08 '22 at 20:54