鱼C论坛

 找回密码
 立即注册
查看: 557|回复: 2

[已解决]代码出了什么错?

[复制链接]
发表于 2023-10-9 15:51:02 | 显示全部楼层 |阅读模式

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

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

x
main.c

#include "sort.c"
#include <stdio.h>
int main()
{
    int arr[] = {5,4,7,34,5,7,2,3,6,9,0,2};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr,n);

}


sort.c

#include <stdio.h>
void sort(int arr[],int n)
{
    for(int j = 1;j < n;j++)
    {
        int i = j - 1;
        int key = arr[j];
        while (i > 0 && arr[i] > key) {
            arr[i + 1] = arr[i];
            i--;
        }
        arr[i + 1] = key;
    }
    for(int a = 0;a < n;a++)
    {
        printf("%d",arr[a]);
    }
}


报错

====================[ 构建 | algorithms | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build "/Volumes/My file/C/github/algorithms/cmake-build-debug" --target algorithms -j 6
[3/3] Linking C executable algorithms
FAILED: algorithms
: && /Library/Developer/CommandLineTools/usr/bin/cc -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/algorithms.dir/main.c.o CMakeFiles/algorithms.dir/sort.c.o -o algorithms   && :
duplicate symbol '_sort' in:
    /Volumes/My file/C/github/algorithms/cmake-build-debug/CMakeFiles/algorithms.dir/sort.c.o
    /Volumes/My file/C/github/algorithms/cmake-build-debug/CMakeFiles/algorithms.dir/main.c.o
ld: 1 duplicate symbols
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
最佳答案
2023-10-9 15:51:55
这个错误是由于存在重复的符号 "_sort" 引起的。重复符号意味着在链接过程中,编译器找到了相同名称的函数或变量定义。

在您的代码中,sort.c 文件中定义了 sort 函数,而 main.c 文件中也包含了 sort.c 文件。这导致在链接时出现重复的符号 "_sort"。

要解决这个问题,可以使用以下两种方法之一:

方法一:将 sort 函数的定义放在头文件中

sort.h
#ifndef SORT_H#define SORT_Hvoid sort(int arr[], int n);#endif
sort.c
#include "sort.h"#include <stdio.h>void sort(int arr[], int n){    // 排序逻辑}
main.c
#include "sort.h"#include <stdio.h>int main(){    int arr[] = {5,4,7,34,5,7,2,3,6,9,0,2};    int n = sizeof(arr) / sizeof(arr[0]);    sort(arr,n);}
在这种方式下,sort 函数的定义被放在 sort.h 头文件中,并被包含在 sort.c 和 main.c 文件中,这样就避免了重复符号的问题。

方法二:只在 main.c 文件中包含 sort.c 文件

main.c
#include <stdio.h>// 在主函数文件中直接定义 sort 函数,不需要另外引入 sort.c 文件void sort(int arr[], int n){    // 排序逻辑}int main(){    int arr[] = {5,4,7,34,5,7,2,3,6,9,0,2};    int n = sizeof(arr) / sizeof(arr[0]);    sort(arr,n);}
在这种方式下,可以直接将 sort 函数的定义放在主函数文件 main.c 中,而不需要包含 sort.c 文件。这样也能解决重复符号的问题。

您可以根据个人偏好选择其中一种方法来解决重复符号的错误。请注意,在使用方法二时,sort 函数的定义应该位于 main 函数之前,或者提前进行函数声明。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-9 15:51:17 | 显示全部楼层
这个错误是因为在链接时出现了重复符号'_sort',即在sort.c和main.c中都定义了函数'sort',导致链接器无法确定应该使用哪个函数。要解决这个问题,可以将sort函数的定义放在一个单独的文件中,例如sort.h,然后在sort.c和main.c中包含sort.h头文件即可。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-9 15:51:55 | 显示全部楼层    本楼为最佳答案   
这个错误是由于存在重复的符号 "_sort" 引起的。重复符号意味着在链接过程中,编译器找到了相同名称的函数或变量定义。

在您的代码中,sort.c 文件中定义了 sort 函数,而 main.c 文件中也包含了 sort.c 文件。这导致在链接时出现重复的符号 "_sort"。

要解决这个问题,可以使用以下两种方法之一:

方法一:将 sort 函数的定义放在头文件中

sort.h
#ifndef SORT_H#define SORT_Hvoid sort(int arr[], int n);#endif
sort.c
#include "sort.h"#include <stdio.h>void sort(int arr[], int n){    // 排序逻辑}
main.c
#include "sort.h"#include <stdio.h>int main(){    int arr[] = {5,4,7,34,5,7,2,3,6,9,0,2};    int n = sizeof(arr) / sizeof(arr[0]);    sort(arr,n);}
在这种方式下,sort 函数的定义被放在 sort.h 头文件中,并被包含在 sort.c 和 main.c 文件中,这样就避免了重复符号的问题。

方法二:只在 main.c 文件中包含 sort.c 文件

main.c
#include <stdio.h>// 在主函数文件中直接定义 sort 函数,不需要另外引入 sort.c 文件void sort(int arr[], int n){    // 排序逻辑}int main(){    int arr[] = {5,4,7,34,5,7,2,3,6,9,0,2};    int n = sizeof(arr) / sizeof(arr[0]);    sort(arr,n);}
在这种方式下,可以直接将 sort 函数的定义放在主函数文件 main.c 中,而不需要包含 sort.c 文件。这样也能解决重复符号的问题。

您可以根据个人偏好选择其中一种方法来解决重复符号的错误。请注意,在使用方法二时,sort 函数的定义应该位于 main 函数之前,或者提前进行函数声明。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 23:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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