首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gcc生产.o,但bash说未知命令

gcc生产.o,但bash说未知命令
EN

Stack Overflow用户
提问于 2011-05-28 07:09:52
回答 5查看 479关注 0票数 3

编辑:我已经修复了前面指出的几个问题,但真正的问题仍然没有解决。

此bash脚本:

代码语言:javascript
复制
set -vx

/usr/bin/llvm-gcc-4.2 -ansi -g -o mytest mytest.c 
ls -l mytest
./mytest
file mytest

生成以下输出:

代码语言:javascript
复制
/usr/bin/llvm-gcc-4.2 -ansi -g -o mytest mytest.c 
++ /usr/bin/llvm-gcc-4.2 -ansi -g -o mytest mytest.c
ls -l mytest
++ ls -l mytest
-rwxr-xr-x  1 jimslager  wheel  37496 May 27 17:26 mytest
./mytest
++ ./mytest
error: unknown command ./mytest
file mytest
++ file mytest
mytest: Mach-O 64-bit executable x86_64

我从一些更大的东西中提取出来,我已经使用了几个月,但从未见过这样的结果。如果一个对象是未知的,那么gcc如何才能生成一个没有错误或警告的对象呢?

如果有人问我,我会发布test.c,但它很长,而且对我来说,我的问题似乎与test.c中的内容无关。

编辑:这是代码。对不起,太长了。

代码语言:javascript
复制
/* Test Exercise 5-10: Write the program myexpr, which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument.  For example,
 *      expr 2 3 4 + *
 * evaluates 2 x (3+4).
 * */
#include <stdio.h>
#include <string.h>
#define MAXLINE 68
#define TABSIZE 8
#define TAB '`'
#define SPACE '-'
#define NEW '\\'
#define TRUE 1
#define FALSE 0
#define IN 1
#define OUT 0
#define FOLDLENGTH 20
#define MAXLEN 12
#define SMALLER(i, j) ((i) > (j) ? (j) : (i))
#define N(c) (c=='\0' ? '\\' : c)

/* #include "subs.c" */

#define MAXOP 100 /* max size of operand or operator */ 
#define NUMBER '0' /* signal that a number was found */

int getop(char []); 
void push(double); 
double pop(void);
double top(void);
void dup(void);
void clear(void);
void stackswap(void);
double atof(char s[]);
int myisdigit(char c);
int myisspace(char c);

/* reverse Polish calculator */ 
int main(int argc, char *argv[]) 
{
    int type; 
    double op2; 
    char s[MAXOP];

    while (--argc>0) 
        while (type = getop(&(*++argv[0])))
printf("Just after while: type = %d, *argv[0] = %s\n", type, *argv);
        switch (type) {
        case NUMBER: 
            push(atof(*argv));
printf("NUMBER: atof(*argv[0]) = %g, *argv[0] = %s\n", atof(*argv), *argv);
            break; 
        case '+':
printf("+ detected\n");
            push(pop() + pop());
            break; 
        case '*':
printf("* detected\n");
            push(pop() * pop()); 
            break; 
        case '-':
printf("- detected\n");
            op2 = pop(); 
            push(pop() - op2);
            break;
        case '/': 
printf("/ detected\n");
            op2 = pop();
            if (op2 != 0.0) 
                push(pop() / op2);
            else
                printf("error: zero divisor\n"); 
            break;
        case '%': 
printf("Modulo detected\n");
            op2 = pop();
            if (op2 != 0.0) 
                push((int) pop() % (int) op2);
            else
                printf("error: zero divisor\n"); 
            break;
        case 't':
printf("t detected\n");
            printf("%g\n", top());
            break; 
        case 'd':
printf("d detected\n");
            dup();

            break; 
        case 's': 
printf("s detected\n");
            stackswap();
            break;
        case 'c':
printf("c detected\n");
            clear();
            break;
        case '\n': 
printf("\\n detected\n");
            printf("\t%.8g\n", pop()); 
            break;
        default: 
            printf("error: unknown command %s\n", *argv); 
            break;
        }
     return 0;
}

#define MAXVAL 100 /* maximum depth of val stack */

int sp = 0; /* next free stack position */ 
double val[MAXVAL]; /* value stack */

/* push: push f onto value stack */ 
void push(double f) 
{
printf("push: Started.   f = %g, sp = %d\n", f, sp);
    if (sp < MAXVAL) 
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
printf("push: Finished.  f = %g, sp = %d\n", f, sp);
}

/* dup: duplicate top of stack */ 
void dup(void) 
{
printf("dup: Started.   top = %g, sp = %d\n", top(), sp);
    push(top());
printf("dup: Finished.   top = %g, sp = %d\n", top(), sp);
}

/* pop: pop and return top value from stack */ 
double pop(void) 
{
printf("pop: sp = %d, val[--sp] = %g\n", sp, val[sp-1]);
    if (sp > 0) 
        return val[--sp];
    else { 
        printf("error: stack empty\n"); 
        return 0.0;
    }
}

/* top: return top value from stack without changing sp */ 
double top(void) 
{
printf("top: sp = %d, val[0] = %g\n", sp, val[0]);
    if (sp > 0) 
        return val[sp-1];
    else { 
        printf("error: stack empty\n"); 
        return 0.0;
    }
}

/* stackswap: swap the top 2 values in stack */
void stackswap(void)
{
printf("Starting stackswap: val[sp-1] = %g, val[sp-2] = %g\n", val[sp-1], val[sp-2]);
    double op2, op3;
    op2 = pop();
    op3 = pop();
    push(op2);
    push(op3);
printf("Finishing stackswap: val[sp-1] = %g, val[sp-2] = %g\n", val[sp-1], val[sp-2]);
}

/* clear: clear the stack */
void clear(void)
{
    sp = 0;
}

int getch(void); 
void ungetch(int);

/* getop: get next character or numeric operand */ 
int getop(char s[]) 
{
    int i, c;

    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0'; 
    if (!isdigit(c) && c != '.')
        return c; /* not a number */
    i = 0;
    if (isdigit(c)) /* collect integer part */ 
        while (isdigit(s[++i] = c = getch()))
            ;
    if (c=='.') /* collect fraction part */
        while (isdigit(s[++i] = c = getch())) ;
    s[i] = '\0'; 
    if (c != EOF)
        ungetch(c); 
    return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];  /* buffer for ungetch */ 
int bufp = 0;   /* next free position in buf */

int getch(void) /* get a (possibly pushed-back) character */ 
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */ 
{
    if (bufp >= BUFSIZE) 
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

/* atof: convert s to double */
double atof(char s[])
{
    double val, power, epower, d; 
    int i, j, sign, esign=0, eval;
printf("atof: s = %s\n", s);

    for (i = 0; myisspace(s[i]); i++); /* skip white space */

    sign = (s[i] == '-') ? -1 : 1; /* Determine sign and strip it */
    if (s[i] == '+' || s[i] == '-')
        i++; 

    for (val = 0.0; myisdigit(s[i]); i++) /* Determine value before dec point */
        val = 10.0 * val + (s[i] - '0'); 

    if (s[i] == '.')
        i++; 

    for (power = 1.0; myisdigit(s[i]); i++) { /* include value after dec */
        val = 10.0 * val + (s[i] - '0'); 
        power *= 10;            /* power is where . goes */
    } 

    if (s[i]=='e' || s[i]=='E') { /* Exponential form */

        esign = (s[++i]=='-') ? -1 : 1; /* Sign of exponent */
        if (s[i]=='+' || s[i]=='-')
            i++;

        for (epower=0.1, eval=0.0; myisdigit(s[i]); i++) { /* Determine exponent */
            eval = 10*eval + (s[i]-'0');
            epower *= 10;
        }
    }

    d = (sign*val / power);     /* Place dec point in mantissa */

    if (esign!=0) {         /* If exp form then adjust exponent */
        for (j=1; j<=eval; j++) {
            d = (esign==1 ? d*10.0 : d/10.0);
        }
    }
    return (d);
}

/* Determine is c is a digit */
int myisdigit(char c)
{
    if (c>='0' && c<='9') 
        return TRUE;
    else 
        return FALSE;
}

/* Returns 1 if c is whitespace, 0 otherwise */
int myisspace(char c)
{
    return ((c==' ' || c=='\n' || c=='\t') ? 1 : 0); 
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-05-28 21:25:32

多!我终于想到了这里发生的事情。本程序是K&R中的练习5-10,它将修改第76页的反向波兰语计算器,以从命令行而不是从标准输入接收输入。当我收到"unknown command“消息时,我正在做这件事,并以为它来自编译器,但实际上它来自我自己的代码!

现在我将返回并修改它,在所有错误消息前加上argv0前缀,这样这种错误就不会再发生了。

票数 0
EN

Stack Overflow用户

发布于 2011-05-28 07:13:43

它可能应该是./test.o来运行它。通常,UNIX系统没有".“(当前目录),默认在路径中。

此外,".o“扩展名有一点误导,因为这是一个中间目标文件的约定,而不是像您在这里生成的独立可执行文件。

票数 6
EN

Stack Overflow用户

发布于 2011-05-28 07:13:59

将最后一行设置为./test.o,一切都会正常工作。

如果您只提供文件名,而不提供路径,那么将只考虑搜索路径,并且在大多数情况下,当前工作目录不是搜索路径的一部分(或者更确切地说,在您的示例中绝对不是)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6158505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档