DNKROZ
DNKROZ

Reputation: 2852

Unresolved externals (LNK1120)

I am getting the error fatal error LNK1120: 3 unresolved externals when compiling my program using Developer CMD Prompt for VS2012.

Each unresolved external symbol has the error: LNK2019

nickman.obj : error LNK2019: unresolved external symbol _scrninit referenced in function _main
nickman.obj : error LNK2019: unresolved external symbol _scrnnrml referenced in function _main
nickman.obj : error LNK2019: unresolved external symbol _scrneng referenced in function _main

When compiling using GCC i get

cannot find -lpthread collect2.exe: error: ld returned 1 exit status

I've been told this is also due to unresolved symbols.

I can't seem to see anything wrong with what I've done, but maybe someone else can. I'm guessing there's a problem with the way that I've included the header file

Here is my code:

nickman.c

#include "nickman.h"

void main(void)
{
    unsigned int  curr_dat = 0; // The current dat file to use
    unsigned char ch = 0;       // Key entered at keyboard
    unsigned char lastkey = 0;  // Last key entered (movement command)

    if (scrninit()) {
        printf("\nThere was an error initializing the screen.");
        exit(-1);
    }

    if (scrneng(curr_dat)) {
        printf("\nThere was an error creating the screen.");
        scrnnrml();
        exit(-2);
  }

    //to leave it on there for us to see
    getch();

   scrnnrml();
}

nickman.h

extern int scrninit();                  // the screen initialize routine
extern     scrnnrml();                  // reset screen to normal
extern int scrneng(unsigned curr_dat);  // the screen display engine routine

screen.c

#include "screen.h"

// the screen initialize routine
int scrninit()
{
    _asm {
        mov  ax,0013h
        int  10h
    }
    return(0);  // for now (no error)
}

void scrnnrml()
{
    _asm {
        mov  ax,0003h
        int  10h
    }
}

void display_item(int item_num, int x, int y)
{
    unsigned int  postn, temp;

    y *= 10;
    x *= 10;
    postn = y * 320 + x;
    temp = item_num * 100;

    _asm {
        push ds
        push es
        push si
        push di
        mov  ax,0A000h
        mov  es,ax
        mov  si,offset itembitmap
        add  si,temp
        mov  di,postn
        mov  ax,seg itembitmap
        mov  ds,ax
        mov  cx,10
loop1:
        push cx
        mov  cx,05
        rep  movsw
        add  di,310
        pop  cx
        loop loop1
        pop  di
        pop  si
        pop  es
        pop  ds
    }
}

scrneng.c

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

#include "scrneng.h"

  // the screen display engine routine
int scrneng(unsigned curr_dat)
{
    FILE *fp;
                     int  i,j;
    unsigned char dat_file[] = "SCRN00.DAT";
    unsigned char scrn_grid[21][33];

    // open, read in the grid, and close the file
    if((fp = fopen(dat_file,"rb"))==NULL) {
        printf("\nError opening file");
        return(1);
    }
    for (i=1;i<=20;i++) {
        for (j=1;j<=32;j++)
            scrn_grid[i][j] = fgetc(fp) - 97;
        fgetc(fp);  // skip CR LF
        fgetc(fp);  //
    }
    fclose(fp);

    for(i=1;i<=20;i++)
        for(j=1;j<=32;j++)
            display_item(scrn_grid[i][j],j-1,i-1);

    return(0); // no error
}

Sorry for the large code dump, didn't really know what to include and exclude!

Upvotes: 2

Views: 1913

Answers (1)

dbush
dbush

Reputation: 223709

You need to specify each file you want to compile on the command line. If you only give the source file that contains main, there's no way for it to know where the other functions live.

For example, if you had screen.c and screen_old.c, both with the same functions but (possibly) different implementations, how would the compiler know which one to use?

You can either compile each one separately to an object file then link the object files:

cl -c nickman.c
cl -c screen.c
cl -c scrneng.c
cl -out:nickman nickman.o screen.o scrneng.o

Or compile and link all at once:

cl -out:nickman nickman.c screen.c scrneng.c

Upvotes: 3

Related Questions