简体   繁体   中英

Language C: basic question regarding calculator function in EdX program

Question; I'm brand new to coding and doing the self taught route; not important. But building the calculator in cs50.

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int x = get_int ("x: ");
    int y = get_int("y: ");
    printf("%i\n", x + y);
}

Is the base set; when I go into my terminal to $ make calculator there's an output of...

make: *** No rule to make target 'calculator'. Stop.

I feel as though I'm missing something very basic here; for what its worth I have the terminal set to the wsl so I can route Ubuntu through this from a previous coding bootcamp suggestion; unsure if this is making things weird..?

make: *** No rule to make target 'calculator'. Stop.

means that there is a line missing in your makefile. Hence here comes a complete example of everything.

Source in standard C without library cs50 :

calculator.c

#include <stdio.h>

// no cs50.h hence using a helper function
int get_int(char * msg) {int n; printf("%s", msg); scanf(" %d", &n); return n;}

int main(void)
{
    int x = get_int ("x: ");
    int y = get_int("y: ");
    printf("%i\n", x + y);
}

Simple makefile (the use of tabulators for indentation is required):

Makefile

default: calculator

calculator.o: calculator.c
    gcc -c calculator.c -o calculator.o

calculator: calculator.o
    gcc calculator.o -o calculator

clean:
    -rm -f calculator.o
    -rm -f calculator

And now let's see:

$ make calculator
gcc -c calculator.c -o calculator.o
cc   calculator.o   -o calculator
$ ./calculator 
x: 1
y: 1
2
$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM