Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
");
}

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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...