Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Problems in creating libraries

Riccardo 'Taro'

New Coder
Hi there! I got a strange error when i was trying to create a library with a function that execute a bubble-sort. The error is this:
" undefined reference to `bubblesort'
collect2.exe: error: ld returned 1 exit status "
I leave the codes of the .h .c and main.c files below.
I think i'm one of the few who got this error-code. If someone here can help me i would be really glad. Thanks!

C:
extern void bubblesort (int, int[]);

C:
#include "bbsort.h"

void bubblesort (int n, int a[]) {
    int i, j, temp;
    for (i=0; i<n-1; i++)
        for (j=0; j<n-1; j++)
            if (a[j]>a[j+1]) {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
}

C:
#include "bbsort.h"
#include <stdio.h>
#define n 8

int main () {
    int a[n];
    
    printf ("insert values in the arrey:\n\t");     //load the arrey
    for (int i=0; i<n; i++) {
        scanf("%d", &a[i]);
        printf ("\t");
    }

    bubblesort(n, a);                         //bubblesort instruction

    for (int i=0; i<n; i++) {                //print the sorted arrey
        printf ("%d\t", a[i]);
    }

    return 0;
}
 

New Threads

Buy us a coffee!

Back
Top Bottom