Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cables

macrumors newbie
Original poster
Jan 5, 2008
3
0
Well, first C program on a Mac, anyway.

My program starts with a line #include <stdio.h>. The rest is a standard program that works on the HP-UX systems at work. I try to compile with the command:

./gcc-4.0 myprog -o myprog.c

and get the error message:

/usr/bin/ld: myprog bad magic number (not a Mach-0 file)
collect2: ld returned 1 exit status

The message suggests a fault with the file type. Any ideas how to fix this? Help would be greatly appreciated.

Mark
 

cables

macrumors newbie
Original poster
Jan 5, 2008
3
0
Thanks for the suggestion.

I've just tried that and got the same error message.

Mark
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Try just doing gcc myFile.c this then creates a file called a.out, which you run with ./a.out

The -o myFile just creates an output file called myFile, which doesn't help really. Also I compile all my source for test purposes without a -o myFile bit.

EDIT: Aside from my suggestion being simpler that RobbieDuncan's there is nothing wrong with his suggestions as far as I can see so I'm puzzled as to why you are still having problems.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I've had a though (dangerous I know). Is there already a file called myprog in the folder that was compiled on an HP-UX system that gcc can't overwrite? Try copying the source only to a new folder and compile there...
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Is the Mac Intel or PPC? If you are doing bitwise operations this can make a difference if the Mac is Intel (as Intel is Little Endian, vs Big Endian for Unix), though compilation should be unaffected.
 

cables

macrumors newbie
Original poster
Jan 5, 2008
3
0
Fixed it!

It turns out all I needed to do was put .c on the end of my program name.

Thanks to everyone for the suggestions.

Mark
 

bokbosky

macrumors newbie
Jul 29, 2008
3
0
Please help

Hi guys,

I want to start programming in C on my MacBook. I tried all the ways I can, to compile the very basic program, but failed. Please help me.

#include <stdio.h>
main()
{
*printf("Hello world \n");
}

Error Message:
dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c
world.c: In function ‘main’:
world.c:4: error: stray ‘\194’ in program
world.c:4: error: stray ‘\160’ in program

Methods followed: 3

1.

I tried writing this code in Textwrangler and then compiled using the option 'Run in the terminal' by adding 'shebang' (#! /bin/sh), code is compiled but could not find the output of printf statement,

Output:
Last login: Wed Jul 30 01:03:19 on ttys001
/var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
dhcp-018-125:~ ram$ /var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
logout

[Process completed]

Method 2 & 3:
Both of these 2 methods are similar in the way of compiling, in both the ways I used terminal, but the codes were written in textwrangler & textedit.

command in the terminal:
dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c

Got the same error message:

world.c: In function ‘main’:
world.c:4: error: stray ‘\194’ in program
world.c:4: error: stray ‘\160’ in program

Guys, sorry for the long message, but all day I tried to compile with all possible ways, but no luck.

Any help will be appreciated.
 

MacDonaldsd

macrumors 65816
Sep 8, 2005
1,005
0
London , UK
Did you put the * in front of printf or is that the compiler ? if so that is the error.

Without it that works fine on my mac
Hi guys,

I want to start programming in C on my MacBook. I tried all the ways I can, to compile the very basic program, but failed. Please help me.

#include <stdio.h>
main()
{
*printf("Hello world \n");
}

Error Message:
dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c
world.c: In function ‘main’:
world.c:4: error: stray ‘\194’ in program
world.c:4: error: stray ‘\160’ in program

Methods followed: 3

1.

I tried writing this code in Textwrangler and then compiled using the option 'Run in the terminal' by adding 'shebang' (#! /bin/sh), code is compiled but could not find the output of printf statement,

Output:
Last login: Wed Jul 30 01:03:19 on ttys001
/var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
dhcp-018-125:~ ram$ /var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
logout

[Process completed]

Method 2 & 3:
Both of these 2 methods are similar in the way of compiling, in both the ways I used terminal, but the codes were written in textwrangler & textedit.

command in the terminal:
dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c

Got the same error message:

world.c: In function ‘main’:
world.c:4: error: stray ‘\194’ in program
world.c:4: error: stray ‘\160’ in program

Guys, sorry for the long message, but all day I tried to compile with all possible ways, but no luck.

Any help will be appreciated.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi guys,

I want to start programming in C on my MacBook. I tried all the ways I can, to compile the very basic program, but failed. Please help me.

#include <stdio.h>
main()
{
*printf("Hello world \n");
}

Error Message:
dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c
world.c: In function ‘main’:
world.c:4: error: stray ‘\194’ in program
world.c:4: error: stray ‘\160’ in program
Don't use a word processor for programming. It looks like someone added some characters that are not part of the C character set. Did you re-type the program or did you actually copy what the compiler tried to compile and paste it in here?

I think you can make life a lot easier for yourself if you just use XCode.
 

bokbosky

macrumors newbie
Jul 29, 2008
3
0
Hey! how did This came up!

Thank you very much for your replies guys.
Sorry guys I could not see that * in the editor. Anyways I wrote that again, here is the error message again,

#include <stdio.h>
main()
{
printf("Hello World!\n");
}

Error Message:

/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
collect2: ld returned 1 exit status

see anything?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
In an editor like vi or pico, enter:
Code:
int main(int argc, char *argv[]) {
  return 0;
}

Save this file as test.c.

From the terminal, in the directory you saved this file, run:
Code:
gcc -o test test.c

Does this compile? Is there an executable file named test in the directory?

If not, what errors do you get? Do not save this file as rich text using some other editor. Use a command line based editor, or if you cannot, find a graphical programmer's text editor (others might have some recommendations). Chances are things are being saved as rich text, and odd things (unicode, high ascii, etc.) are being saved in your source file, and the compiler or linker is choking on them.

-Lee
 

cruzrojas

macrumors member
Mar 26, 2007
67
0
USA
Thank you very much for your replies guys.
Sorry guys I could not see that * in the editor. Anyways I wrote that again, here is the error message again,

#include <stdio.h>
main()
{
printf("Hello World!\n");
}

Error Message:

I believe the error is in the second line. main have to be a function that return an integer. so change main() to int main() and maybe add a return 0; after printf but that is not necessary.

Hope this helps. best
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Thank you very much for your replies guys.
Sorry guys I could not see that * in the editor. Anyways I wrote that again, here is the error message again,

#include <stdio.h>
main()
{
printf("Hello World!\n");
}

Error Message:

/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
collect2: ld returned 1 exit status

see anything?

As I said, XCode would make your life easier.
 

Berlepsch

macrumors 6502
Oct 22, 2007
303
48
Error Message:

/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
collect2: ld returned 1 exit status

see anything?

The good thing is, that the compiler is happy with your program now. The bad news is, the linker ("ld") is complaining now. My guess is, that it has found the compiled code for your main() function, and it would like to reserve memory for the return value of your function now. The problem is, you didn't tell what kind of variable your return value should be, so the compiler skipped this part, and the linker can't find it out on its own.
With all the answers in this thread, it should be easy for you to correct this mistake, so I don't have to spell it out again.
 

JVene

macrumors newbie
Jul 22, 2008
29
0
>>As I said, XCode would make your life easier.


How so? You still get the same error messages.


In one of the 'standard' books on 'professional' programming, they cover this topic with a very valid viewpoint: use a good IDE (and XCode definately qualifies).

With the command line tools, even with multiple terminal windows to avoid the 'leave the editor to compile-return to editor to correct' workflow, one is still in a rather direct drive approach to the compiler and tools. There's way too much to learn for the novice (lots of switches, libraries to include, etc.) - even the professionals don't want to have to think about these things.

A good IDE will solve this problem by presenting an 'interface' to the project that deals with the issue of compiling and linking, even presenting error messages in a more convenient fashion. Learning is a bit faster, and working is quite a bit faster, than with command line tools.

I worked in Unix for years, even before Mac or Windows, and before the common advent of the IDE. I'm very glad to put that experience behind me. Familiarity with command line tools can be of some value, but in the last 10 years I can't recall when I ever needed to resort to it.

I would consider it a separate study for the student, and rely on the IDE to 'package' the experience of compiling and linking into something more productive and gentler.
 

theg sprank

macrumors newbie
Jul 30, 2008
3
0
My guess is, that it has found the compiled code for your main() function, and it would like to reserve memory for the return value of your function now. The problem is, you didn't tell what kind of variable your return value should be...

No. The default return value when one is unspecified is int. His program is perfectly valid, and compiles and runs on my machine.
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
With the command line tools, even with multiple terminal windows to avoid the 'leave the editor to compile-return to editor to correct' workflow, one is still in a rather direct drive approach to the compiler and tools. There's way too much to learn for the novice (lots of switches, libraries to include, etc.) - even the professionals don't want to have to think about these things.

Heh, I think the complete opposite. The IDE makes everything much harder as you don't know what it is doing explicitly. As a beginner that makes it extremely hard to understand what is going on as everything is hidden from them. With the command line tools everything is so much simpler, you enter a command you know what it does and you know what the results are meant to be therefore it is easier to debug. In an IDE you are never quite sure what it is actually doing under the hood, so you don't know if it is a problem with your code or if you have made a mistake in setting up the IDE.

But as with most things this is a matter of opinion. I learnt using the command line tools. I'm still not comfortable in Xcode really to be truthful because it hides so much of what it is doing.

His program is perfectly valid.

No it isn't. C99 expressly forbids implicit return types.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
By definition there can only ever be one standard.

There's the C Standard, and there's the language that the gcc compiler compiles. They are not the same thing. The default for the gcc compiler is some mixture of C90 plus C99 features but with plenty of backwards compatibility. Using compiler switches you can make it more like C99, but not one hundred percent.

Anyway, if you use XCode, create a new project, choose "Command Line Utility", then "Standard Tool", you won't have to figure out strange linker problems. You'll also find it a lot easier to create a debugging version, actually use the debugger, do things like creating a PowerPC or 64 bit version, etc. etc. etc.
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
There's the C Standard, and there's the language that the gcc compiler compiles. They are not the same thing.

Of course. I never claimed GCC supported C99 in its entirety. All I said was that claiming it was valid code is incorrect, because in order for it to be valid it must adhere to the current C standard.

I only know of one C compiler that supports C99 properly and that is the Intel compiler.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.