2012-02-22 92 views
2

我是新來的C/C + +,我想我有一些基本問題。編譯時,我得到undefined reference to u_fopen_48'錯誤:未定義的引用'u_fopen_48'

#include <unicode/ustdio.h> 

int main(int argc, char** argv) { 
    UFILE* ufile = u_fopen("/home/emstol/Desktop/utf8demo.txt", "r", NULL, "utf8"); 
    return 0; 
} 

文件此功能是here。我正在使用ICU 4.8.1(根據readme.html一步一步編譯我自己)),下面有g ++的NetBeans。如果這有助於我在建設過程中看到:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf 
make[1]: Entering directory `/home/emstol/NetBeansProjects/TextFairy1' 
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/textfairy1 
make[2]: Entering directory `/home/emstol/NetBeansProjects/TextFairy1' 
mkdir -p dist/Debug/GNU-Linux-x86 
g++  -o dist/Debug/GNU-Linux-x86/textfairy1 build/Debug/GNU-Linux-x86/main.o 
build/Debug/GNU-Linux-x86/main.o: In function `main': 
/home/emstol/NetBeansProjects/TextFairy1/main.cpp:4: undefined reference to `u_fopen_48' 
collect2: ld returned 1 exit status 
make[2]: *** [dist/Debug/GNU-Linux-x86/textfairy1] Error 1 
make[2]: Leaving directory `/home/emstol/NetBeansProjects/TextFairy1' 
make[1]: *** [.build-conf] Error 2 
make[1]: Leaving directory `/home/emstol/NetBeansProjects/TextFairy1' 
make: *** [.build-impl] Error 2 

回答

3

您似乎忘記了鏈接您使用的庫。有關說明,請參閱This page

構建複合項目時,編譯器不能輕易找到所需的所有引用。大多數庫都以共享對象文件(.so)的形式出現,並且沒有將它們的C代碼與您的項目的其餘部分一起編譯,同時僅爲它們的函數提供頭文件。這允許編譯器在代碼中爲要放入的函數創建「套接字」,但不告訴鏈接器應將這些函數從何處取出 - 鏈接過程將會失敗。 因此,您必須明確告訴鏈接程序在哪裏搜索它將要搜索的符號,這通常是用-l標誌完成的,儘管看起來ICU庫已經採取了與它不同的方法。

+1

正確!感謝您快速回答。爲你+1。 – emstol 2012-02-22 21:11:29