简体   繁体   中英

How I solve this simple problem with CodeBlock?

I'm a beginner and have 3 file to sum recursive an array.

array.h

int sum(int n, int a[]);
void create_array(int n, int a[]);

array.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;
}

PROBLEMS

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
undefined reference to `create_array'|
undefined reference to `sum'|
||error: ld returned 1 exit status|

I have put these 2 files in the same directory. If I compile and run them with terminal cmd Windows they started. But if I try to compile from IDE CodeBlocks says this errors. Anyone help?

There's a problem with the way you've told Code::Blocks that these files are all related.

What I did:

■ opened up the IDE and clicked NewFile->Empty File

■ copied the first block of code, pasted it and saved as array.h

■ copied the second block of code, pasted it and saved as array.c

■ copied the third block of code, pasted it and saved as main.c

■ Hit "Build and Run"

Then got exactly the same set of errors that you did.

What you needed to do instead, was hit New File->Project, select Console Application, select C, enter a project name (the exe will have the same name by default, though you can change this) From there, you'll get a folder created that contains a.CBP file and a main.c file. You can then hit "New File" and this time, it will ask you if you want to add it to the current project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM