【发布时间】:2013-08-11 18:33:18
【问题描述】:
尽管我对 C++ 很陌生,但我并不完全理解这种链接之类的东西。
我认为这是关于extern "C" 链接。
extern "C"
{
int loadbmp(char *filename, unsigned char **buf,
int *w, int *h, int pf, int bottomup);
const char *bmpgeterr(void);
}
unsigned char *srcBuf=NULL, **jpegBuf=NULL;
unsigned long jpegsize=0;
int width, height;
char *filename={"Screenshot158139.bmp"};
tjhandle handle=NULL;
void main(){
if(loadbmp(filename, &srcBuf, &width, &height,TJPF_RGB, 0)==-1){
//printf("Could not load bitmap: %s\n", bmpgeterr());
exit(1);
}
if((handle=tjInitCompress())==NULL) {
printf("Could not initialize compressor: %s\n", tjGetErrorStr());
free(srcBuf);
exit(1);
}
if((tjCompress2(handle, srcBuf, width, 0, height, TJPF_RGB,
jpegBuf, &jpegsize, TJSAMP_444,10, 0))==-1) {
printf("Could not compress: %s\n", tjGetErrorStr());
free(&srcBuf);
tjDestroy(handle);
exit(1);
}
}
我从中得到的问题是我需要解决我认为的extern "C" 代码:
error LNK2001: unresolved external symbol loadbmp
遗憾的是,我不知道该怎么做,而且由于此错误在 C++ 世界中极为常见,因此找到答案并不容易,因为它们可能会有所不同。
希望解决这个问题很容易,因为我想我必须将它定义为外部代码。
【问题讨论】:
-
如果我没记错的话,来自 C++ 标准的第 7 节。
-
main()的唯一可移植返回类型是int。 -
只是一般提示:文字字符串是常量,因此您应该将指向文字字符串的变量声明为
const char* filename = "...";或像char filename[] = "...";中的数组. -
至于您的问题,您在构建时缺少与文件链接,无论是库文件还是目标文件。
-
@user2587718 - 所有 c/c++ 程序需要链接。