gcc生成.o,但bash表示未知命令

如何解决gcc生成.o,但bash表示未知命令

编辑:我已经修复了已指出的几件事,但实际问题仍未解决。 这个bash脚本:
set -vx

/usr/bin/llvm-gcc-4.2 -ansi -g -o mytest mytest.c 
ls -l mytest
./mytest
file mytest
产生以下输出:
/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中的内容无关。 编辑:这是代码。抱歉,太长了。
/* 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,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);
}

/* dup: duplicate top of stack */ 
void dup(void) 
{
printf(\"dup: Started.   top = %g,top(),sp);
    push(top());
printf(\"dup: Finished.   top = %g,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\",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]);
}

/* 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); 
}
    

解决方法

        可能应该是./test.o来运行它。通常,缺省情况下,UNIX系统的PATH中没有\“。\”(当前目录)。 同样,“。o”扩展名也会引起误解,因为这是中间目标文件的约定,而不是您在此处生成的独立可执行文件。     ,        最后一行line3ѭ,一切正常。 如果仅提供文件名而不提供路径,则仅考虑搜索路径,并且在大多数情况下,您当前的工作目录不是其中的一部分(在您的示例中,肯定不是)。     ,        
.
不在
$PATH
中,因此您需要指定文件的路径。
./test.o
.o
文件通常不可执行;必须先链接它们,然后才能运行它们。尽管这里不是这种情况。     ,        本地目录可能不在您的路径中(也不希望它在目录中)。您应该可以用
./test.o
运行它。另外,这里的后缀
.o
很奇怪。您有一个动态链接的可执行文件,而不是目标文件。 (尝试
file test.o
。)在Unix上,这些通常没有扩展名,在Windows上,它们通常具有扩展名“11ѭ”。     ,        h!最终,这件事发生在我身上。该程序是K&R的练习5-10,它将修改第76页的反向波兰语计算器,以从命令行而不是从stdin接收其输入。当我收到“未知命令”消息并认为它来自编译器时,我正在执行此操作,但实际上它来自我自己的代码! 现在,我将返回并对其进行修改,以在所有错误消息前添加argv [0](并从现在开始使用),以使该错误不再发生。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-