未解决的外部因素(LNK1120)

时间:2016-09-02 13:19:54

标签: c visual-studio-2012 linker-errors lnk2019

使用Developer CMD Prompt for VS2012编译我的程序时收到错误fatal error LNK1120: 3 unresolved externals

每个未解析的外部符号都有错误: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

使用GCC进行编译时,我得到了

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

我被告知这也是由于未解决的符号。

我似乎没有看到我所做的事情有什么不妥,但也许别人可以。我猜测我包含头文件

的方式存在问题

这是我的代码:

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
}

对于大型代码转储感到抱歉,我真的不知道要包括和排除什么!

1 个答案:

答案 0 :(得分:3)

您需要在命令行中指定要编译的每个文件。如果您只提供包含main的源文件,则无法知道其他函数的位置。

例如,如果你有screen.cscreen_old.c,两者具有相同的功能但(可能)不同的实现,编译器如何知道使用哪一个?

您可以将每个单独编译为目标文件,然后链接目标文件:

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

或者一次编译和链接:

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