[英]How I solve this simple problem with CodeBlock?
我是一个初学者,有 3 个文件来对一个数组求和。
数组.h
int sum(int n, int a[]);
void create_array(int n, int a[]);
数组.c
#include<stdio.h>
#include<stdlib.h>
#include "array.h"
void create_array(int n, int a[]){
int i;
for( i=0;i<n;i++){
printf("Inserire il %d elemento dell'array\n",i);
scanf("%d",&a[i]);
}
return;
}
int sum(int n, int a[]){
if(n>0){
return a[n-1]+sum(n-1,a);
}
return 0;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "array.h"
int main()
{
printf("Inserire la dimensione dell'array\n");
int n;
scanf("%d",&n);
int a[n];
create_array(n,a);
int res=sum(n,a);
printf("La somma degli elementi e' %d\n", res);
return 0;
}
问题
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
undefined reference to `create_array'|
undefined reference to `sum'|
||error: ld returned 1 exit status|
我已将这两个文件放在同一个目录中。 如果我使用终端 cmd Windows 编译并运行它们,它们就会启动。 但是,如果我尝试从 IDE CodeBlocks 编译,则会出现此错误。 有人帮忙吗?
您告诉 Code::Blocks 这些文件都是相关的方式存在问题。
■ 打开 IDE 并点击 NewFile->Empty File
■ 复制第一块代码,粘贴并保存为array.h
■ 复制第二块代码,粘贴并保存为数组。c
■ 复制第三块代码,粘贴并保存为main.c
■ 点击“构建并运行”
你需要做的,是点击新文件->项目,select控制台应用程序,select C,输入项目名称(exe可以更改),但默认情况下你可以从那里获得相同的名称创建的文件夹包含一个.CBP 文件和一个 main.c 文件。 然后您可以点击“新建文件”,这一次,它会询问您是否要将其添加到当前项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.