鱼C论坛

 找回密码
 立即注册
查看: 274|回复: 4

[已解决][Error] ld returned 1 exit status 出现这个怎么解决

[复制链接]
发表于 2024-4-10 12:08:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 256
long total;

int countLines(const char *filename);
void findALLCodes(const char *path);
void findALLFiles(const char *path);

int countLines(const char *filename)
{
        FILE *fp;
        int count = 0;
        int temp;
        if ((fp=fopen(filename,"r"))==NULL);
         {       
                 fprintf(stderr,"Can not open the file:%s\n",filename);
                 return 0;
          }
          
          while((temp=fgetc(fp))!=EOF)
          {
                          if (temp=='\n')
                          {
                                  count++;
                                 
                          }
          }
          fclose(fp);
          return count;
}

void findALLCodes (const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath[MAX],target[MAX];
       
        strcpy(thePath,path);
        if((handle = _findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
        {
                do
                {
                        sprintf(target,"%s/%s",path,fa.name);
                        total += countLines(target);
                }while(_findnext(handle,&fa)==0);
                 }
                _findclose(handle);
         }
void findAllDirs(const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath[MAX];
       
        strcpy(thePath,path);
        if((handle =_findfirst(strcat(thePath,"/*"),&fa))==-1L)
        {
                fprintf(stderr,"The path %s is wrong!\n",path);
                return;
        }
        do{
                if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
                continue;
               
                if(fa.attrib == _A_SUBDIR)
                {
                        sprintf(thePath,"%s,%s",path,fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }
               
        }while (_findnext(handle,&fa)==0);
        _findclose(handle);
}
int main ()
{ char path[MAX]=".";

printf("计算中...\n");

findALLCodes(path);
findALLDirs(path);
printf("目前你总共写了%ld行代码!\n\n",total);
system("pause");
return 0;
}
最佳答案
2024-4-10 14:08:21
本帖最后由 jackz007 于 2024-4-10 16:13 编辑

        检查一下编译命令,应该这么写:
gcc -o x x.c
        如果写成了:
gcc x x.c
        就会收到这样的错误信息。
        建议楼主试一下这个代码:
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define MAX 256
long total                                                                            ;

long countLines(const char * filename)
{
        FILE * fp                                                                     ;
        long count = 0 , temp                                                         ;
        if ((fp = fopen(filename , "r"))) {
                while((temp = fgetc(fp)) != EOF) if (temp == '\n') count ++           ;
                fclose(fp)                                                            ;
        } else {
                fprintf(stderr , "[错误]:无法打开文件:%s\n" , filename)             ;
        }
        return count                                                                  ;
}

void findAllCodes(const char * path)
{
        struct _finddata_t fa                                                         ;
        long handle , c , m                                                           ;
        char thePath[MAX] , target[MAX]                                               ;
        strcpy(thePath , path)                                                        ;
        strcat(thePath , "\\*")                                                       ; // 生成搜索目标字符串
        if((handle = _findfirst(thePath , & fa)) != -1L) {
                do {
                        for(m = 0 ; fa . name[m] ; m ++)                              ; // 取得字符串 fa 的长度 m   
                        sprintf(target , "%s\\%s" , path , fa . name)                 ; // 为 fa 生成磁盘路径 target
                        if(fa . attrib == _A_SUBDIR) {                                  // 如果 fa 是子目录
                                if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
                                        findAllCodes(target)                          ; // 递归
                                }                                
                        } else {                                                        // 如果 fa 是普通文件
                                if(! stricmp(& fa . name[m - 2] , ".c") || ! stricmp(& fa . name[m - 2] , ".h") || ! stricmp(& fa . name[m - 4] , ".cpp")) {                 
                                        c = countLines(target)                        ; // 统计代码行数
                                        printf("[%60s] : %ld\n" , target , c)         ; // 屏显文件代码行数信息
                                        total += c                                    ; // 代码总行数累加
                                }
                        }
                } while(! _findnext(handle , & fa))                                   ;
                _findclose(handle)                                                    ;
        }
}

int main(int argc , char * argv[])
{
        char path[MAX]                                                                ;
        if(argc > 1) strcpy(path , argv[1])                                           ; // 如果有命令行,就作为搜索起始目录
        else getcwd(path , MAX)                                                       ; // 否则,以当前路径作为搜索起始目录
        printf("计算中 ...\n")                                                        ;
        findAllCodes(path)                                                            ;
        printf("目前你总共写了 %ld 行代码!\n" , total)                               ;
        system("pause")                                                               ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-10 12:09:10 | 显示全部楼层
这个错误通常意味着链接器无法解析所有的符号引用,导致无法生成可执行文件。在你的代码中,有几个问题可能导致了这个错误。

第一个问题是在countLines函数的定义中,你错误地在语句if ((fp=fopen(filename,"r"))==NULL); 的末尾多了一个分号。这个分号使得下面的代码块成为一个独立的语句,它总是会执行并返回0。正确的写法应该是if ((fp=fopen(filename,"r"))==NULL) {},不需要分号。

第二个问题是在findALLCodes函数中,你没有为变量thePath和target分配足够的空间来存储路径和文件名。你可以将它们声明为字符数组,如char thePath[MAX]和char target[MAX]。

第三个问题是在findAllDirs函数中,函数名被拼写错了。它应该是findAllDirs而不是findAllDirs。

最后一个问题是在main函数中,你将变量path声明为一个字符,但实际上应该声明为一个字符数组,如char path[MAX]。

下面是修改后的代码:
#include <stdio.h>
#include <string.h>
#include <io.h>

#define MAX 256
long total;

int countLines(const char *filename);
void findALLCodes(const char *path);
void findAllDirs(const char *path);

int countLines(const char *filename)
{
    FILE *fp;
    int count = 0;
    int temp;
    if ((fp=fopen(filename,"r"))==NULL)
    {   
        fprintf(stderr,"Can not open the file:%s\n",filename);
        return 0;
    }
    
    while((temp=fgetc(fp))!=EOF)
    {
        if (temp=='\n')
        {
            count++;
        }
    }
    fclose(fp);
    return count;
}

void findALLCodes (const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath[MAX],target[MAX];
    
    strcpy(thePath,path);
    if((handle = _findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
    {
        do
        {
            sprintf(target,"%s/%s",path,fa.name);
            total += countLines(target);
        }while(_findnext(handle,&fa)==0);
    }
    _findclose(handle);
}

void findAllDirs(const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath[MAX];
    
    strcpy(thePath,path);
    if((handle =_findfirst(strcat(thePath,"/*"),&fa))==-1L)
    {
        fprintf(stderr,"The path %s is wrong!\n",path);
        return;
    }
    do{
        if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
            continue;
        
        if(fa.attrib == _A_SUBDIR)
        {
            sprintf(thePath,"%s,%s",path,fa.name);
            findALLCodes(thePath);
            findAllDirs(thePath);
        }
        
    }while (_findnext(handle,&fa)==0);
    _findclose(handle);
}

int main ()
{
    char path[MAX];

    printf("计算中...\n");

    strcpy(path, ".");
    findALLCodes(path);
    findAllDirs(path);
    printf("目前你总共写了%ld行代码!\n\n",total);
    system("pause");
    return 0;
}

希望这次能够成功编译和运行!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-10 12:09:43 | 显示全部楼层
感觉和原代码一样啊,不知道哪里出问题了,复制原代码可以运行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-10 12:09:57 | 显示全部楼层
丢的FC 发表于 2024-04-10 12:09
感觉和原代码一样啊,不知道哪里出问题了,复制原代码 ...


非常抱歉,我给出的代码确实没有修改。 让我重新修复一下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>

#define MAX 256

long total;

int countLines(const char *filename);
void findALLCodes(const char *path);
void findAllDirs(const char *path);

int countLines(const char *filename)
{
    FILE *fp;
    int count = 0;
    int temp;
    if ((fp = fopen(filename, "r")) == NULL)
    {
        fprintf(stderr, "Can not open the file:%s\n", filename);
        return 0;
    }

    while ((temp = fgetc(fp)) != EOF)
    {
        if (temp == '\n')
        {
            count++;
        }
    }
    fclose(fp);
    return count;
}

void findALLCodes(const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath[MAX], target[MAX];
    strcpy(thePath, path);

    if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
    {
        do
        {
            sprintf(target, "%s/%s", path, fa.name);
            total += countLines(target);
        } while (_findnext(handle, &fa) == 0);
    }
    _findclose(handle);
}

void findAllDirs(const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath[MAX];
    strcpy(thePath, path);

    if ((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
    {
        fprintf(stderr, "The path %s is wrong!\n", path);
        return;
    }

    do
    {
        if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
            continue;

        if (fa.attrib == _A_SUBDIR)
        {
            sprintf(thePath, "%s/%s", path, fa.name);
            findALLCodes(thePath);
            findAllDirs(thePath);
        }
    } while (_findnext(handle, &fa) == 0);
    _findclose(handle);
}

int main()
{
    char path[MAX];
    printf("计算中...\n");
    strcpy(path, ".");
    findALLCodes(path);
    findAllDirs(path);
    printf("目前你总共写了%ld行代码!\n\n", total);
    system("pause");
    return 0;
}

这次代码应该能够编译和运行成功了。希望能解决你的问题!如果仍然存在问题,请提供具体的错误提示信息,我将尽力帮助你解决。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-10 14:08:21 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2024-4-10 16:13 编辑

        检查一下编译命令,应该这么写:
gcc -o x x.c
        如果写成了:
gcc x x.c
        就会收到这样的错误信息。
        建议楼主试一下这个代码:
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define MAX 256
long total                                                                            ;

long countLines(const char * filename)
{
        FILE * fp                                                                     ;
        long count = 0 , temp                                                         ;
        if ((fp = fopen(filename , "r"))) {
                while((temp = fgetc(fp)) != EOF) if (temp == '\n') count ++           ;
                fclose(fp)                                                            ;
        } else {
                fprintf(stderr , "[错误]:无法打开文件:%s\n" , filename)             ;
        }
        return count                                                                  ;
}

void findAllCodes(const char * path)
{
        struct _finddata_t fa                                                         ;
        long handle , c , m                                                           ;
        char thePath[MAX] , target[MAX]                                               ;
        strcpy(thePath , path)                                                        ;
        strcat(thePath , "\\*")                                                       ; // 生成搜索目标字符串
        if((handle = _findfirst(thePath , & fa)) != -1L) {
                do {
                        for(m = 0 ; fa . name[m] ; m ++)                              ; // 取得字符串 fa 的长度 m   
                        sprintf(target , "%s\\%s" , path , fa . name)                 ; // 为 fa 生成磁盘路径 target
                        if(fa . attrib == _A_SUBDIR) {                                  // 如果 fa 是子目录
                                if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
                                        findAllCodes(target)                          ; // 递归
                                }                                
                        } else {                                                        // 如果 fa 是普通文件
                                if(! stricmp(& fa . name[m - 2] , ".c") || ! stricmp(& fa . name[m - 2] , ".h") || ! stricmp(& fa . name[m - 4] , ".cpp")) {                 
                                        c = countLines(target)                        ; // 统计代码行数
                                        printf("[%60s] : %ld\n" , target , c)         ; // 屏显文件代码行数信息
                                        total += c                                    ; // 代码总行数累加
                                }
                        }
                } while(! _findnext(handle , & fa))                                   ;
                _findclose(handle)                                                    ;
        }
}

int main(int argc , char * argv[])
{
        char path[MAX]                                                                ;
        if(argc > 1) strcpy(path , argv[1])                                           ; // 如果有命令行,就作为搜索起始目录
        else getcwd(path , MAX)                                                       ; // 否则,以当前路径作为搜索起始目录
        printf("计算中 ...\n")                                                        ;
        findAllCodes(path)                                                            ;
        printf("目前你总共写了 %ld 行代码!\n" , total)                               ;
        system("pause")                                                               ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-27 22:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表