2015-05-06 33 views
0

我們的教授提供了以前任務的目標文件,我無法完成。完成/測試當前作業需要完成此前的作業。是否有可能以某種方式將它們導入到eclipse中或以某種方式使我的項目與這些目標文件一起工作?是否可以在eclipse中導入/運行目標文件?

+0

你還需要頭文件和函數/類聲明。 – NikolayKondratyev

+0

謝謝,我會在稍後問教授。是否只需將對象文件拖放到與我的程序相同的源文件夾中? – AlldaRage

回答

0

比方說,你有目標文件print_hello.a和標題print_hello.h。更精確地說,讓我們創建print_hello.a

print_hello.h

#ifndef __PRINT_HELLO_ 
#define __PRINT_HELLO_ 

void print_hello(); 

#endif /* __PRINT_HELLO__ */ 

print_hello.c

#include <stdio.h> 
#include "print_hello.h" 

void print_hello() { 
    printf("Hello!\n"); 
} 

與編譯

$ gcc -c print_hello.c -o print_hello.a 

現在我們需要將其添加到Eclipse。創建一個項目,我們稱之爲example。創建一個example.c中,你會打電話給print_hello

#include "print_hello.h" 

int main() { 
    print_hello(); 
} 

現在我們需要將它鏈接到print_hello.a。右鍵點擊項目並選擇Properties。去C/C++ Build -> Settings -> GCC C Linker -> Miscellaneous。在Other objects點擊添加按鈕,然後選擇print_hello.a的路徑。還要在GCC C Compiler -> Includes中添加.h文件的路徑。建立和運行你的項目,它應該輸出

Hello! 
相關問題