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

Futhark

macrumors 65816
Original poster
Jun 12, 2011
1,238
179
Northern Ireland
I'm a total newbie to programming and i've been learning how to program in C and using MacVim as my code editor and GCC to compile my programs. I completed a very basic app of 9kb in size that i want to share with family and friends. I emailed them the Unix Executable File that GCC compiled for me but no one can get it to work? they are all Mac users i've sent this to, what have i done wrong? :confused:

One person emailed me back with a screen shot and i can see terminal is opened with this error message:

Illegal Instruction: 4
logout

[Process completed]

I hope this helps
 
I'm a total newbie to programming and i've been learning how to program in C and using MacVim as my code editor and GCC to compile my programs. I completed a very basic app of 9kb in size that i want to share with family and friends. I emailed them the Unix Executable File that GCC compiled for me but no one can get it to work? they are all Mac users i've sent this to, what have i done wrong? :confused:

One person emailed me back with a screen shot and i can see terminal is opened with this error message:

Illegal Instruction: 4
logout

[Process completed]

I hope this helps

Code?
What Frameworks did you use?
Did you give them instructions on how to run it from, say, the command line?
How did you bundle it, zip?
When they 'installed' it did the permissions and execute bit work out ok?

EDIT: Also, when you say 'Unix Executable File' I assume you mean a Mac Mach-O binary.
 
Code?
What Frameworks did you use?
Did you give them instructions on how to run it from, say, the command line?
How did you bundle it, zip?
When they 'installed' it did the permissions and execute bit work out ok?

EDIT: Also, when you say 'Unix Executable File' I assume you mean a Mac Mach-O binary.

this is my very first attempt at an app and i've no idea how i should have distributed it so it runs on fellow family and friends Mac machines.

what i did was write my code in MacVim and used GCC to compile it. when you say Framework would that be the #include <stdio.h> that i typed at the top? sorry if thats a silly response but i'm trying my best :eek:

When i compiled my app i typed this:

gcc myapp.c -o myapp

This created the UNIX executable file i was talking about and it is this file i sent to my family and friends, I thought it would be as simple as double clicking the executable and it would run my app but obviously not :confused:

When i emailed it to them i just attached the file, i didn't zip it because it was only 9kb in size. I hope this makes sense to you and you can guide me in what i've done wrong and how i should fix it.

Many Thanks
 
If your program only links to <stdio.h> where is the harm in just posting the code?

All I could do is guess, that either A: you have a bug in your code. B: something went wrong in the compilation step. C: you compiled with 10.6+ and your friend is on <=10.5.

Otherwise zip up your program and post it here. *note that if he does this you probably shouldn't run the program, instead use gdb to step through or decompile the application.
 
I would say it is one of three things:

1. Permissions Error
2. They are on a different version of OS X than you (Snow Leopard instead of Lion for example)
3. Bug in your source code
 
If your program only links to <stdio.h> where is the harm in just posting the code?

All I could do is guess, that either A: you have a bug in your code. B: something went wrong in the compilation step. C: you compiled with 10.6+ and your friend is on <=10.5.

Otherwise zip up your program and post it here. *note that if he does this you probably shouldn't run the program, instead use gdb to step through or decompile the application.

Jared thanks for the replies but please realise i'm only new to programming so i wouldn't know what's safe to post and what isn't yet lol as for my family and friends they have limited computer experience, if you can't click it and run it then it's going to be too much work to try and explain to them what to do, compiling etc I know my code is bug free as it runs perfectly on my machine and even runs when i click on the file created by GCC as i thought it would on their machines but isn't the case. (I even copy & pasted my code into Xcode and it showed up with no errors)

So what does their computers need to have installed that will make this work? I assumed the only requirement was that they had a Mac because i compiled my program on a Mac? Once double clicked the program would open in terminal, how wrong am i lol :D

----------

I would say it is one of three things:

1. Permissions Error
2. They are on a different version of OS X than you (Snow Leopard instead of Lion for example)
3. Bug in your source code

1. Permissions Error what is this and what can i do to see if this is the issue and how would i go about fixing it?

2. Ahhhhh i'm on Mountain Lion 10.8 they're on Lion, My code is REALLY BASIC :eek: would this really make a difference?

3. As above my code is SOOOO BASIC even i couldn't mess it up LOL (I Hope)
 
You're not going to get much help if you're not going to post your code.

If you're using Mountain Lion (I can't imagine why you would be) then that's the problem.
 
You're not going to get much help if you're not going to post your code.

If you're using Mountain Lion (I can't imagine why you would be) then that's the problem.

I have talked a friend through the process on how to run the app in terminal and he's getting the Permission Denied that you mentioned, I can post the code if you wish, I'm a little embarrassed to because of how crap it is :eek:
 
chmod the file to 700 and chown it to his user account

If you need more information on File Permissions in OS X, check out this document: http://support.apple.com/kb/HT2963

Try using the '-arch i386' argument when building your program if he still can't run it
 
I have talked a friend through the process on how to run the app in terminal and he's getting the Permission Denied that you mentioned, I can post the code if you wish, I'm a little embarrassed to because of how crap it is :eek:

Then compile the plain ordinary Hello World C program. Give that to your friend. Post that code. Also post the complete error messages (copy and paste from Terminal window).

When something fails, you should know how to make a simple isolated test case that can show the problem, so someone else can try to duplicate the problem. This is a basic debugging skill. If the isolated test case doesn't show the problem, then you've learned something.
 
chmod the file to 700 and chown it to his user account

If you need more information on File Permissions in OS X, check out this document: http://support.apple.com/kb/HT2963

Try using the '-arch i386' argument when building your program if he still can't run it

Thank you for all your help it's much appreciated. So lets say i wrote a program that was actually going to be useful to lots of people what procedure would i follow so that it could be distributed for best compatibility amongst all types of macs? i hope i'm making sense
 
Thank you for all your help it's much appreciated. So lets say i wrote a program that was actually going to be useful to lots of people what procedure would i follow so that it could be distributed for best compatibility amongst all types of macs? i hope i'm making sense

Step #1. Read all the advice, e.g. I brought up permissions in my first post.
Step #2. Use Xcode. There will be more build options, more target options, the ability to compile multiple targets at once (for multiple OS's) etc. Yes all (most??) of these are available as command line or makefile stuff, but you don't really know all of them do you??


That said, since we don't know WHAT your program does it is hard to tell what the issues are. e.g permissions could be that your code is trying to write to /etc/hosts OR it could be writing to /Users/SuperAdmin/Desktop and is not able to because A: there is no user, and B: the program doesn't have privileges to make the folder.

EDIT:
Step #3. Learn from this so that in the future you can say, "oh you can't run my command line application by clicking it, here is a laundry list of things to check."
 

Attachments

  • Screen Shot 2012-07-06 at 1.51.40 PM.png
    Screen Shot 2012-07-06 at 1.51.40 PM.png
    141.8 KB · Views: 137
Last edited:
Ok here's my code as simple as it is :eek: i've put it into Xcode which i'm not terribly familiar with and i see in the project a few settings referring to 10.8 Base SDK & OS X Deployment Target, i've set these to 10.7 so what do i do now? how do i make my program distributable to the world :D

#include <stdio.h>
int main()
{

#define TAX 20

float monday,tuesday,wednesday,thursday,friday,saturday,sunday;
char ch;
puts("\n*** My Wages Calculator ***\n");

float rateOfPay;
printf("What is your hourly pay rate before tax?\n");
scanf("%f",&rateOfPay);

printf("How many hours did you work on Monday?\n");
scanf("%f",&monday);
printf("How many hours did you work on Tuesday?\n");
scanf("%f",&tuesday);
printf("How many hours did you work on Wednesday?\n");
scanf("%f",&wednesday);
printf("How many hours did you work on Thursday?\n");
scanf("%f",&thursday);
printf("How many hours did you work on Friday?\n");
scanf("%f",&friday);
printf("How many hours did you work on Saturday?\n");
scanf("%f",&saturday);

/* Hours Worked in a week depending on a 6 or 7 day week. */

float result1 = (monday+tuesday+wednesday+thursday+friday+saturday);
float pay6 = result1*rateOfPay;
float tax6 = (pay6/100*TAX);

fpurge(stdin);

printf("Do you work on a Sunday? (Y/N)?\n");

ch = getchar();
if(ch == 'N' || ch == 'n')
{
printf("\nThis week you worked:\n%.2f hours!\n\n",result1);
printf("You earned £%.2f before tax\n",result1*rateOfPay);
printf("After tax deduction you earned £%.2f\n\n",(pay6-tax6));
}
else if(ch == 'Y' || ch == 'y')
{
printf("How many hours did you work on Sunday?\n");
scanf("%f",&sunday);

float result2 = (monday+tuesday+wednesday+thursday+friday+saturday+sunday);
float pay7 = result2*rateOfPay;
float tax7 = (pay7/100*TAX);

printf("\nThis week you worked:\n%.2f hours!\n\n",result2);
printf("You earned £%.2f before tax\n",result2*rateOfPay);
printf("After tax deduction you earned £%.2f\n\n",(pay7-tax7));
}
return(0);
}
 
Ok here's my code as simple as it is :eek: i've put it into Xcode which i'm not terribly familiar with and i see in the project a few settings referring to 10.8 Base SDK & OS X Deployment Target, i've set these to 10.7 so what do i do now? how do i make my program distributable to the world :D

Are you saying that your friend is using 10.7 and still cannot run the program?
I would suggest zipping it (right click-> compress) and sending it to your friend that way. Some transmission methods are not necessarily binary safe with any file format, using something like a dmg or zip file is the answer to that.

Make sure your friend puts it on their desktop or something so they for sure have privileges in the unpacked folder.

Now for some unsolicited style advice.
#1. Things like int main() vs int main(int argc, char **argv) and return(0) vs return 0
They may seem archaic and magical but it is a good idea to use them and learn them anyway. (btw, the number and type of arguments is important for linking properly I'm not saying that is your problem just be aware of it. and return is not a function, it is a keyword you wrote return(0), you could have wrote return (((0))) for all the compiler cares. Like foo = 5+(6). Sometimes you see ( ) around a longer expression after return. Almost always with a space after, and ultimately whatever the expression evaluates to is what is returned.)

#2. Whenever you find yourself getting up to variableName# where # is above 2 or 3, you might want to think about are they really necessary. In this case, the variables monday-saturday could just be replaced by a counter like 'totalHours' that you add to at each step of the program.

#3. Some of the repeat typing could be simplified with functions, which I'm going to assume you haven't really gotten around to.

Here is a slight rewrite just to see some of these in work. (also note the [ code ] block)

Code:
#include <stdio.h>

#define kTAXRATE 0.2f

float howManyHours(const char *);
int workedSunday(void);

int main (int argc, const char * argv[]) {
    float rateOfPay, pay, tax;
    float hours = 0;

    printf("\n*** My Wages Calculator ***\n");

    printf("What is your hourly pay rate before tax?\n");
    scanf("%f", &rateOfPay);
    
    hours += howManyHours("Monday");
    hours += howManyHours("Tuesday");
    hours += howManyHours("Wednesday");
    hours += howManyHours("Thursday");
    hours += howManyHours("Friday");
    hours += howManyHours("Saturday");
    
    if(workedSunday()) {
        hours += howManyHours("Sunday");
    }
    
    pay = hours * rateOfPay;
    tax = pay * kTAXRATE;
    
    printf("\nThis week you worked:\n%.2f hours!\n\n", hours);
    printf("You earned £%.2f before tax\n", pay);
    printf("After tax deduction you earned £%.2f\n\n", pay - tax);
    
    return 0;
}

float howManyHours(const char *s) {
    float x;
    printf("How many hours did you work on %s?\n", s);
    scanf("%f", &x);
    return x;
}

int workedSunday() {
    char ch;
    while (1) {
        fpurge(stdin);
        printf("Do you work on a Sunday? (Y/N)?\n");
        ch = getchar();
        if (ch == 'Y' || ch == 'y') {
            return 1;
        } else if (ch == 'N' || ch == 'n') {
            return 0;
        }
    }
}
*note the workedSunday() function doesn't bail if they enter a non Y/N answer. In practice you might want to write a counter into it to let the user out if they keep messing up.
 
Are you saying that your friend is using 10.7 and still cannot run the program?
I would suggest zipping it (right click-> compress) and sending it to your friend that way. Some transmission methods are not necessarily binary safe with any file format, using something like a dmg or zip file is the answer to that.

Make sure your friend puts it on their desktop or something so they for sure have privileges in the unpacked folder.

Now for some unsolicited style advice.
#1. Things like int main() vs int main(int argc, char **argv) and return(0) vs return 0
They may seem archaic and magical but it is a good idea to use them and learn them anyway. (btw, the number and type of arguments is important for linking properly I'm not saying that is your problem just be aware of it. and return is not a function, it is a keyword you wrote return(0), you could have wrote return (((0))) for all the compiler cares. Like foo = 5+(6). Sometimes you see ( ) around a longer expression after return. Almost always with a space after, and ultimately whatever the expression evaluates to is what is returned.)

#2. Whenever you find yourself getting up to variableName# where # is above 2 or 3, you might want to think about are they really necessary. In this case, the variables monday-saturday could just be replaced by a counter like 'totalHours' that you add to at each step of the program.

#3. Some of the repeat typing could be simplified with functions, which I'm going to assume you haven't really gotten around to.

Here is a slight rewrite just to see some of these in work. (also note the [ code ] block)

Code:
#include <stdio.h>

#define kTAXRATE 0.2f

float howManyHours(const char *);
int workedSunday(void);

int main (int argc, const char * argv[]) {
    float rateOfPay, pay, tax;
    float hours = 0;

    printf("\n*** My Wages Calculator ***\n");

    printf("What is your hourly pay rate before tax?\n");
    scanf("%f", &rateOfPay);
    
    hours += howManyHours("Monday");
    hours += howManyHours("Tuesday");
    hours += howManyHours("Wednesday");
    hours += howManyHours("Thursday");
    hours += howManyHours("Friday");
    hours += howManyHours("Saturday");
    
    if(workedSunday()) {
        hours += howManyHours("Sunday");
    }
    
    pay = hours * rateOfPay;
    tax = pay * kTAXRATE;
    
    printf("\nThis week you worked:\n%.2f hours!\n\n", hours);
    printf("You earned £%.2f before tax\n", pay);
    printf("After tax deduction you earned £%.2f\n\n", pay - tax);
    
    return 0;
}

float howManyHours(const char *s) {
    float x;
    printf("How many hours did you work on %s?\n", s);
    scanf("%f", &x);
    return x;
}

int workedSunday() {
    char ch;
    while (1) {
        fpurge(stdin);
        printf("Do you work on a Sunday? (Y/N)?\n");
        ch = getchar();
        if (ch == 'Y' || ch == 'y') {
            return 1;
        } else if (ch == 'N' || ch == 'n') {
            return 0;
        }
    }
}
*note the workedSunday() function doesn't bail if they enter a non Y/N answer. In practice you might want to write a counter into it to let the user out if they keep messing up.


Nice rewrite, but I have to throw in that using fpurge() is not recommended because it is not a standard part of the C library, and probably won't work on other systems.
 
I can't thank you all enough for the work you've done here for me !!! I am just about to learn about functions so it is nice to see how you implemented them into my own app.
 
Nice rewrite, but I have to throw in that using fpurge() is not recommended because it is not a standard part of the C library, and probably won't work on other systems.

I didn't really know what it did, but since it was part of the original I decided to err on the side of caution and keep it rather than simply throwing it away or learning more about it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.