-1

I am trying to compile and run my first code in C.

I used this code:

HelloC.h

#include<stdio.h>

// added int to prevent warning
int main()
{
    printf("Hello World");

}

I tried compiling with these instructions...

gcc -Wall HelloC.h -o HelloC
chmod +x HelloC

...and this top answer (same thing but replace gcc with clang).

The Problem: In both cases ./HelloC returns exec format error: ./HelloC.

My Setup

OSX: 10.12.4

clang --version

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

gcc --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Community
  • 1
  • 1
Matt Groth
  • 151
  • 1
  • 2
  • 9
  • 2
    Why is your code in a `.h` file? – Scott Hunter May 14 '17 at 00:43
  • How did you decide to use `chmod`? That isn't part of the instructions you link to. – Scott Hunter May 14 '17 at 00:45
  • 1) Code should reside in `.c` files, not `.h`. 2) Functions that take no arguments should look like `func_name(void)` to avoid being called with arbitrary amounts of arguments. 3) You should actually `return` something from `main`. 4) Your compiler should make the output file executable automatically - if you ever have to use `chmod`, that's a bad sign. – Siguza May 14 '17 at 00:45
  • Thanks, see my answer. I think I misread something else when I was naming the file, but now I understand. I used `chmod` because trying to run the .h file gave me a permission error, and I was going to worry about that later but turns out that was part of the same problem. – Matt Groth May 14 '17 at 00:54
  • @Siguza About 2), I have a Java background where void cannot be placed as a parameter and I'm perfectly comfortable with leaving the space between the parenthesis blank. Is it just an old convention or does it affect compilation or runtime in any way? – Matt Groth May 14 '17 at 00:59
  • @MattGroth Yes it does affect compilation. See [this question](https://stackoverflow.com/q/693788) and [this example](https://ghostbin.com/paste/tk9fs) that compiles with no error or warning. C treats `()` as "unspecified" rather than "no arguments". – Siguza May 14 '17 at 01:11
  • I see, and so passing arguments to a method with unspecified parameters causes no error. Thanks for the explaination. – Matt Groth May 14 '17 at 01:32
  • 1
    @Siguza: A `return 0;` at the end of `main` is unnecessary as of C99. – Keith Thompson May 14 '17 at 01:34
  • 1
    This is no way releated to gcc. Read the messages, just because you use the command `gcc` does not mean it **is** gcc. (Blame Apple for this stupid confusion!) – too honest for this site May 14 '17 at 03:21
  • @Olaf I'm aware now that most of my question was irrelevant. Would it be better for me to remove the Setup section and simplify the rest? – Matt Groth May 14 '17 at 04:31

1 Answers1

0

Credit to @ScottHunter and @Siguza

I needed to use .c not .h as the file extension.

Matt Groth
  • 151
  • 1
  • 2
  • 9