Is it possible to import/run object files in eclipse?

心不动则不痛 提交于 2019-11-27 08:46:11

问题


Our professor made available object files of a previous assignment that I wasn't able to complete. This previous assignment is required for completing/testing the current assignment. Would it be possible somehow for me to import them into eclipse or somehow make my project work with those object files?


回答1:


Let's say you have object file print_hello.a and a header print_hello.h. To be more precise let's create 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");
}

Compile it with

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

Now we need to add this to Eclipse. Create a project let's call it example. Create a example.c in which you will call print_hello

#include "print_hello.h"

int main() {
    print_hello();
}

Now we need to link it to the print_hello.a. Right-click on project and choose Properties. Go to the C/C++ Build -> Settings -> GCC C Linker -> Miscellaneous. In the Other objects click on add button and choose the path to the print_hello.a. Also add path to .h file in GCC C Compiler -> Includes. Build and run you project, it should output

Hello!


来源:https://stackoverflow.com/questions/30086902/is-it-possible-to-import-run-object-files-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!