-2

I have the following header file:

#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED

typedef struct {
    double r; //real part
    double i; //imag part
} complex;

complex make(double r,double i);

#endif // COMPLEX_H_INCLUDED

and .c file:

#include <stdio.h>
#include <math.h>
#include "complex.h"

complex make(double re,double im)
{
    complex z;
    z.r=re;
    z.i=im;
    return z
}

Now, when I try to create a complex in a main file, I don't seem to be able to print a complex number that I've created.

#include <stdio.h>
#include <stdlib.h>
#include "complex.h"

int main()
{
    double a,b;
    printf("Enter real, then imaginary part:");
    scanf("%f %f",a,b);

    complex z;
    z=make(a,b);

    printf("The number is: %f%+fi",z.r,z.i);
    return 0;
}

I get an error: undefined reference to make.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455

1 Answers1

4

The "undefined reference" error comes from the fact that you're not linking the oobject file produced by compiling complex.c into your executable. You need to add it to your linking command line or project settings, depending on how you develop. We have a FAQ that deals with this aspect of linking in great detail: What is an undefined reference/unresolved external symbol error and how do I fix it? (The FAQ is for C++, but this part of linking is the same in C).


In addition to this, however, you code has the following issue:

Building with warnings enabled (which you should always, always do) produces these warnings (among others) from your code:

main.cpp: In function 'main':

main.cpp:28:13: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Wformat=]
     scanf("%f %f",a,b);
            ~^     ~
main.cpp:28:16: warning: format '%f' expects argument of type 'float *', but argument 3 has type 'double' [-Wformat=]
     scanf("%f %f",a,b);
           ~^    ~

[Live example]

You're passing doubles where a float* is expected, so the program has Undefined Behaviour (and, as in the live example above, will most likely crash).

The correct form is to use pointers as arguments, and format specifiers for the correct type (lf for double):

scanf("%lf %lf",&a,&b);

[Live example]

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • This is not the problem, the problem is "undefines reference to make". –  Oct 10 '18 at 09:00
  • @Hicher, what? Your question does not state that problem in any way! – hellow Oct 10 '18 at 09:01
  • 1
    @Hicher Not according to your question. You claim that "it will take my inputs, ...". If it's running, it must have linked successfully (no linker errors such as undefined references). – Angew is no longer proud of SO Oct 10 '18 at 09:02
  • Your second live version works just fine, also on my computer.. but in that version, no .c file is defined, right? why is this? –  Oct 10 '18 at 09:14
  • @Hicher Because that online compiler doesn't support more than one source file, AFAIK. I've edited the answer to address your question's edit. – Angew is no longer proud of SO Oct 10 '18 at 09:16
  • Now I get a new error: request for member 'i' in something not a structure or union. I tried writing z->r instead of z.r, but it still wouldn't work, why could this be? –  Oct 10 '18 at 09:21
  • Sorry for asking stupid questions, but I am super new to programming and some things that seem so logical to others don't seem to get to me ... –  Oct 10 '18 at 09:24
  • @Hicher Please stop editing the question and invalidating existing answers, that's not how SO works. And also please note that SO is not a "fix my code for me" service. If you're so new, following a good tutorial or book might be a better idea as it will give you overall understanding and not piecemeal answers. – Angew is no longer proud of SO Oct 10 '18 at 09:26
  • @Hicher Also, as the live examples in my answer ([and this one](http://coliru.stacked-crooked.com/a/59b1ee849e45d103)) show, the error with unrecgnised `i` is not reproducible with code in the question. If you're really getting it, double-check everything, do some research etc. and if it's still persistent, you could post it as a new SO question. – Angew is no longer proud of SO Oct 10 '18 at 09:29