0

I am currently implementing complex numbers in C. Somehow this error occurs if i want to run the programm:

edit: to compile I used:

gcc complex.c

./a.exe

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users...\Temp\ccwM3Yew.o:complex.c:(.text+0x184): undefined reference to `addC'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users...\Temp\ccwM3Yew.o:complex.c:(.text+0x1a0): undefined reference to `printComplex' collect2.exe: error: ld returned 1 exit status

Main:

#include "library.h"
#include <stdio.h>

int main () {
    
    complex x, y;

    printf("type in re-part of first complex number:\n");
    scanf("%lf", &x.re);

    printf("type in im-part of first complex number:\n");
    scanf("%lf", &x.im);

    printf("type in re-part of second complex number:\n");
    scanf("%lf", &y.re);
    printf("type in im-part of second complex number:\n");
    scanf("%lf", &y.im);

    complex z = addC(x, y);

    printComplex(x);

    return 0;
}

library.h:

typedef struct {
    double re;
    double im;
} complex; 

complex addC(complex x, complex y);

complex subC(complex x, complex y);

complex divC(complex x, complex y);

complex multC(complex x, complex y);

void printComplex(complex a);

library.c:

#include "library.h"
#include <stdio.h>

complex addC(complex x, complex y) {
     complex z;
     z.re = (x.re + y.re);
     z.im = (x.im + y.im);
     return z;
}

complex subC(complex x, complex y) {
    complex z;
     z.re = (x.re - y.re);
     z.im = (x.im - y.im);
     return z;
}

complex divC(complex x, complex y) {
    //ToDo 
}

complex multC(complex x, complex y) {
    complex z;
     z.re = ((x.re*y.re)-(x.im*y.im));
     z.im = ((x.re*y.im)+(x.im*y.re));
     return z;
}

void printComplex(complex a) {
    printf("Die komplexe Zahl lauetet: %.3f + %.3fi", &a.re, &a.im);
}

thanks:)

  • Make sure that you are compiling (and linking) both `complex.c` and `library.c`. How did you compile the program? Edit to include the command in your question. – kotatsuyaki Oct 30 '22 at 14:35
  • This might be the problem. I have added the information above. – Enno Mühlbauer Oct 30 '22 at 15:07
  • `gcc complex.c` - You are not compiling `library.c` alongside with `complex.c`. [`#include "library.h"` in C merely copies and pastes the content](https://stackoverflow.com/q/5735379/12122460) of the included `library.h` file into `complex.c`, and does nothing (because it can't) to inform the compiler to compile additional files (in this case `library.c`) required. To fix it, compile with the command `gcc complex.c library.c`. – kotatsuyaki Oct 30 '22 at 15:14

0 Answers0