0

I started my adventure with programming today and faced some troubles. While wiritng

#include <stdio.h>

main(void)
{
printf ("hello, world\n");
}

in C language on the website called cs50.io and then writing clang hello.c I've got following error:

enter image description here

Any ideas how to solve it?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • 6
    Please don't post errors as images. Paste them as text in the question. – BackSlash Jun 12 '17 at 22:13
  • 4
    First, try `int main(void)` – Justin J. Jun 12 '17 at 22:14
  • 5
    Should be unrelated to your problem (which seems to be a faulty installation of Clang?) but the `main` function should be explicitly declared to return an `int`. You don't need a `return` statement at the end, but the declaration of `main` should really be correct (`int main(void)`). – Some programmer dude Jun 12 '17 at 22:15
  • 1
    As for your problem, if you use `gcc` instead of `clang` does it work then? And how did you install Clang? – Some programmer dude Jun 12 '17 at 22:18
  • I changed it to ( int main(void) ) but didn't help – Jerry Zerek Jun 12 '17 at 22:19
  • gcc isn't wotking either. Well, I didn't install Clang. How can I do that? – Jerry Zerek Jun 12 '17 at 22:20
  • 2
    That's a problem with the compiler setup rather than with your code. That is, your code shouldn't generate those error messages. It might generate complaints about the code, or it might generate an executable, but it shouldn't be complaining about 'invalid relocations'. That's not your fault. – Jonathan Leffler Jun 12 '17 at 22:21
  • 1
    Have you tried [***searching***](https://www.google.se/search?q=clang+relocation+has+invalid+symbol+index) for e.g. `clang relocation has invalid symbol index`? You should be getting quite a few hits, including many duplicates here in stackoverflow.com. – Some programmer dude Jun 12 '17 at 22:23
  • 1
    I'm voting to close this question as off-topic because this appears to be a problem with a third-party website. You need to take this up with the maintainers of cs50.io; only they can help you. – zwol Jun 12 '17 at 23:08
  • 1
    To be clear, _this is not a problem with your code_. The only problem with your code is that you wrote `main(void)` instead of `int main(void)`, and that should not have produced the error messages you got. (The error message you _should_ have gotten is `warning: type specifier missing, defaults to 'int'`.) – zwol Jun 12 '17 at 23:11
  • The problem seems to be in the compiler. Try re-installing it or trying a different compiler. Refer - [Getting Started with Clang](https://clang.llvm.org/get_started.html) – Trijeet Ganguly Jun 12 '17 at 22:18

2 Answers2

0

First, you could find your answers already posted on StackOverflow. You should read your error messages, and then try to understand what they mean through your own searches.

undefined reference to 'main' error is explained here: Undefined reference to main - collect2: ld returned 1 exit status

Essentially, in C language, void is not a valid return for main() function (i.e. void main(void)). With gcc compiler, which I think, is similar to clang, when main() has no specification, your return defaults to int. So, you have main() function with no return value, when a return is needed. Read this other StackOverflow Q & A for more information on valid signatures for C's main() function:

What are the valid signatures for C's main() function?

Since, I'm not familiar with clang compiler, there may be something else happening besides these errors.

0

As good practice, you will want to compile using flags to account for certain errors/warnings that you may have in your code but might be dismissed by the compiler.

-Werror is a great flag that counts all warnings as errors. You would recompile as clang -Werror hello.c

In this case, since you are just printing and not returning anything, you should have void main (void)

With the code you have provided, you should always account for what the function is returning as mentioned above.

Cheers.