2

I'm coding binary search in helpers.c currently, but I receive a strange clang error every time I try to compile code. I'm absolutely sure my code is logically and syntaxily correct. Where am I going wrong? The error goes as follows:

clang -ggdb3 -O0 -std=c99 -Wall -Werror helpers.c -lcs50 -lm -o helpers 
/usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crt1.o: In function _start': 
(.text+0x18): undefined reference to 'main' 
clang: error: linker command failed with exit code 1 (use -v to see invocation)
6

helpers.c has no main function. You need a main function in every C program, it's the program's starting point, so to speak.

Anyway, the problem with your command line argument is you're missing the file containing the main function - namely find.c - in your list of source files.

Andrej

| improve this answer | |
2

To deepen what Andrej correctly said, consider helpers.c as an auxiliary file where helper functions reside.

When you write complex code, you can distribute your fnctions into different files: this way, each file will contain only those files inherent to a defined scope or matter.

As an example, imagine a project that handles picture files. You will have:

  1. The main source file
  2. A source file for the i/o (low level read and write on files
  3. A source file for the GUI
  4. A source file for the header handling
  5. A source file for the math routines (filters)
  6. A source file that handles errors.

Keeping your code into different files helps both readability and debugging. The only drawback is that you must give all your source files to the compiler.

Luigi

| improve this answer | |
  • Even after I included find.c in helpers, I had to rename the binary_search bool to make the code work. Thank you. – Girish Jun 9 '14 at 14:32
  • Oops. I don't have to include find in helpers. Do I? I have to make find which will make helpers also! Gotcha! – Girish Jun 9 '14 at 14:39
  • You don't have to literally include it: the compiler will take care of it i the definition of the function used is correct. – Cygni_61 Jun 9 '14 at 14:49
2

To deepen what Andrej and Cygni_61 both correctly said, well, you need to 'make find' instead of 'make helpers', which I assume is what you were doing from the clang command. In the Makefile, you can see that running make find will compile find.c, helpers.c, and helpers.h.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .