Andrew
Andrew

Reputation: 1

getting the error code of LNK2001: unresolved external symbol when I run my program

Im getting this error for my code:

1>a1ms4.obj : error LNK2001: unresolved external symbol _name

1>a1ms4.obj : error LNK2001: unresolved external symbol _address

1>a1ms4.obj : error LNK2001: unresolved external symbol _numbers

So my program has 3 files:

a1ms4.c , contacts.c , contacts.h

I run into the error above when i try to run my program.

My program is this

a1ms4.c:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "contacts.h"
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
//       to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:



int main(void)
{
    // Declare variables here:
    
    extern struct Name name;
    extern struct Address address;
    extern struct Numbers numbers;

    
    // Create a variable of type Contact and call it something self-describing like "contact"
    // - HINT: Be sure to initialize the values to 0 and empty C strings
    extern struct Contact contact;

    // Display the title
    printf("Contact Management System\n");
    printf("-------------------------\n\n");
    
    // Call the Contact function getName to store the values for the Name member
    getName(&name);

    // Call the Contact function getAddress to store the values for the Address member
    getAddress(&address);

    // Call the Contact function getNumbers to store the values for the Numbers member
    getNumbers(&numbers);

    // Display Contact summary details
    
    //havent dont this yet

    // Display Completion Message
    printf("Structure test for Contact using functions done!\n");
    
    return 0;
}

contacts.c:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>


// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
//       to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"


// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:

void getName(struct Name *contactName) {

    char choice;

    printf("Please enter the contact's first name: ");
    scanf("%c", (*contactName).firstName);
    printf("\n");

    printf("Do you want to enter a middle intial(s)? (y or n): ");
    scanf("%c", &choice);

    printf("\n");


    if (choice == 'y') {
        printf("Please enter the contact's middle intial(s): ");
        scanf("%c", (*contactName).middleInitial);
        printf("\n");
    }

    else {

        printf("Please enter the contact's last name: ");
        scanf("%c", (*contactName).lastName);
        printf("\n");

    }

}


    // Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
    void getAddress(struct Address *contactAddress){

        char choice;

        printf("Please enter the contact's street number: ");
        scanf("%d", (*contactAddress).streetNumber);
        printf("\n");

        printf("Please enter the contact's street name: ");
        scanf("%c", (*contactAddress).street);
        printf("\n");

        printf("Do you want to enter an apartment number? (y or n): ");
        scanf("%c", &choice);

        printf("\n");


        if (choice == 'y') {
            printf("Please enter the contacts apartment number: ");
            scanf("%d", (*contactAddress).apartmentNumber);
            printf("\n");
        }

        printf("Please enter the contact's postal code: ");
        scanf("%c", (*contactAddress).postalCode);
        printf("\n");


        printf("Please enter the contact's city: ");
        scanf("%c", (*contactAddress).city);
        printf("\n");
    
    }




// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
    void getNumbers(struct Numbers *contactNumber) {
        char choice[5];

        printf("Do you want to enter a cell phone number? (y or n) ");
        scanf("%c", &choice[0]);



        if (choice[0] == 'y') {
            printf("\n");
            printf("Please enter the contact’s cell phone number: ");
            scanf("%d", (*contactNumber).cell);
            printf("\n");

        }

        if (choice[0] == 'n') {
            printf("\n");
        }

        printf("Do you want to enter a home phone number? (y or n) ");
        scanf("%c", &choice[1]);

        if (choice[1] == 'y') {
            printf("\n");
            printf("Please enter the contact’s home phone number: ");
            scanf("%d", (*contactNumber).home);
            printf("\n");
        }

        if (choice[1] == 'n') {
            printf("\n");
        }

        printf("Do you want to enter a home business number? (y or n) ");
        scanf("%c", &choice[3]);

        if (choice[2] == 'y') {
            printf("\n");
            printf("Please enter the contact’s business phone number: ");
            scanf("%d", (*contactNumber).home);
            printf("\n");
        }

        if (choice[2] == 'n') {
            printf("\n");
        }


    }
    

And lastly:

contacts.h:

// Structure type Name declaration (Milestone 1)
struct Name {
    char firstName[31];
    char middleInitial[7];
    char lastName[36];
};

// Structure type Address declaration 
// Place your code here... (from Milestone 1)

struct Address {
    char street[41];
    int streetNumber[1];
    int apartmentNumber[1];
    char postalCode[8];
    char city [41];
};



// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
    int cell[21];
    int home[21];
    int business[21];
};




// Structure type Contact declaration
// Place your code here... (from Milestone 3)

    struct Contact{
        char name;
        char address;
        int number;
    };
    
    
    //------------------------------------------------------
    // Function Prototypes
    //------------------------------------------------------
    
    // ====== Milestone 4 =======
    
    // Get and store from standard input the values for Name
    // Place your code here...
    void getName(struct Name *);
    
    
    // Get and store from standard input the values for Address
    // Place your code here...
    void getAddress(struct Address *);
      
    // Get and store from standard input the values for Numbers
    // Place your code here...
    
    void getNumbers(struct Numbers *);

Upvotes: 0

Views: 252

Answers (1)

Bo Persson
Bo Persson

Reputation: 92261

When you say extern struct Name name; it means "there is a variable name defined in some other file". But there really isn't! That is what the linker complains about - it cannot find the variables in any file.

If you just remove the extern, the variables will be defined inside main, and you can use them right there.

Upvotes: 2

Related Questions