0

I'm trying to run my program, but it keeps giving an error.

I have 3 files and will include the code from one of the 3 functions that are not working.

File: contacts.h

struct Name{
    char firstName[31];
    char middleInitial[7];
    char lastName[36];
};

struct Contact{
    struct Name name;
};

// function prototype:
void getName(struct Name *);

File: contacts.c

#include <stdio.h>
#include “contacts.h”

void getName(struct Name *name)
{
    printf(“Please enter the contact’s first name: “);
    scanf(“%30[&\n]”, name->firstName);   
}

File:a1ms4.c

#include <stdio.h>
#include “contacts.h”

int main(void)
{
    struct Contact contact = { {0} };
    getName(&contect.name);
    printf(“Contact first name: %s\n”, contact.name.firstName);

    return 0;
}

Any idea why I'm getting this error:

a1ms4.c:(.text+0x53): undefined reference to `getName'
  • 1
    How you build ? compile something like this `gcc -Wall a1ms4.c contacts.c -o conatact` – ntshetty Dec 07 '17 at 04:36
  • 2
    What is the full compilation command you are using? The way I read that error is that you've told the compiler that `getName...` is coming (via `contacts.h`) but never actually compiled the function or linked it in. – hunteke Dec 07 '17 at 04:36
  • 4
    You probably did not compile and link contacts.c – M.M Dec 07 '17 at 04:38
  • 4
    `“%30[&\n]”` is a bizarre format string – M.M Dec 07 '17 at 04:38
  • I'm using an windows IDE, dev-c++. Visual studio does the same errors aswell. contacts.c is compiled, and don't have to include it in main. It works through the pointers of contacts.h. Teacher did not ask for us to include contacts.c in a1ms4.c That format string is just to make sure no more than 30 chars are saved, and that it saves up to the newline(enter) irrc – Travis Bradbury Dec 07 '17 at 04:51
  • the object code for contacts is not getting linked in. Why that isn't happening has to do with how you are compiling and linking, which in this case has to do with your IDE. If dev-c++ is anything like Visual Studio or QT (what I've used), there are project files that manage all the source and header files of the project. Is `contacts.c` part of the project? There's not enough info here to see. The stylized quotes make it look like you're writing your code in MS Word or something. – yano Dec 07 '17 at 05:06
  • consider adding the dev-c++ tag since you're using it .. but the tag info for dev-c++ say older versions are buggy and suggest using something else. – yano Dec 07 '17 at 05:07
  • Please note @M.M post... Please explain your intent with that scanf() argument. – TonyB Dec 07 '17 at 05:09
  • those strings like `“%30[&\n]”` are not correct. Replace smart quotes with normal quotes – phuclv Dec 07 '17 at 05:35
  • You haven't correctly created your visual studio project. Make sure that all of your source and header files have been explicitly added to the project. – bruceg Dec 07 '17 at 05:46
  • You have not specified what "%30[&\n]" argument (with correct quotes) to scanf() is intended to perform... I ask because I am not using Windows, which may have extensions to scanf(). – TonyB Dec 07 '17 at 06:23
  • Related https://stackoverflow.com/q/12573816/694576, if not a duplicate to. – alk Dec 07 '17 at 07:23

2 Answers2

0

Probably you are compiling it the wrong way. Try this gcc -c a1ms4.c gcc -c contacts.c gcc -Wall a1ms4.o contacts.o -o contact

HrishiB
  • 41
  • 2
  • I'm using an windows IDE, dev-c++. Visual studio does the same errors aswell. – Travis Bradbury Dec 07 '17 at 04:58
  • @TravisBradbury This is happens if you not mention proper `tag`. Edit question with all these details, then you will get the proper response, Secondly you need to add `contacts.c` as depended file to `a1ms4.c`,So that it will build before main – ntshetty Dec 07 '17 at 05:34
0

I just hope

#include “contacts.h”

is actually

#include "contacts.h"

Remember, is not the same as "

The same goes for this one:

scanf(“%30[&\n]”, name->firstName);

and these too:

printf(“Please enter the contact’s first name: “);
printf(“Contact first name: %s\n”, contact.name.firstName);

Make sure that there is only one version of contacts.h in your include path and you have included the exact file you intend to.

WedaPashi
  • 3,561
  • 26
  • 42