未解决的外部问题 (LNK1120)
Unresolved externals (LNK1120)
我在使用 VS2012 的 Developer CMD Prompt 编译我的程序时收到错误 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
}
对于大量代码转储感到抱歉,真的不知道要包含和排除什么!
您需要在命令行中指定要编译的每个文件。如果您只提供包含 main
的源文件,它就无法知道其他函数的位置。
例如,如果您有 screen.c
和 screen_old.c
,两者具有相同的功能但(可能)不同的实现,编译器如何知道使用哪一个?
您可以将每个单独编译成目标文件,然后 link 目标文件:
cl -c nickman.c
cl -c screen.c
cl -c scrneng.c
cl -out:nickman nickman.o screen.o scrneng.o
或一次编译并 link:
cl -out:nickman nickman.c screen.c scrneng.c
我在使用 VS2012 的 Developer CMD Prompt 编译我的程序时收到错误 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
}
对于大量代码转储感到抱歉,真的不知道要包含和排除什么!
您需要在命令行中指定要编译的每个文件。如果您只提供包含 main
的源文件,它就无法知道其他函数的位置。
例如,如果您有 screen.c
和 screen_old.c
,两者具有相同的功能但(可能)不同的实现,编译器如何知道使用哪一个?
您可以将每个单独编译成目标文件,然后 link 目标文件:
cl -c nickman.c
cl -c screen.c
cl -c scrneng.c
cl -out:nickman nickman.o screen.o scrneng.o
或一次编译并 link:
cl -out:nickman nickman.c screen.c scrneng.c