2

I have a problem with .o file in linux as follow:

I have Visual.cpp which call function ECL_Drawrect() and this function is defined as:

#define ECL_Drawrect ECL_bDrawrect

In my Visual.cpp I also include "ecl.h" as:

#include "ecl.h"

int main() {
    ECL_Drawrect (0,0,20,20,false);
    return 0;
}

The problem is that the function ECL_bDrawrect is in the ecl.o file and i don't know how to link it to use the function mentioned above.

After some research I figured out and setup as follow:

g++ Visual.cpp -o Visual /usr/include/ecl.o

The ecl.o path also incluced in my project and when I compiled i get this error:

**** Build of configuration Debug for project Visual ****

make all 
Building file: ../src/Visual.cpp
Invoking: GCC C++ Compiler
g++ -m32 -O0 -g3 -Wall -c -fmessage-length=0 /usr/include/ecl.o -MMD -MP -MF"src/Visual.d" -MT"src/Visual.d" -o "src/Visual.o" "../src/Visual.cpp"
g++: /usr/include/ecl.o: linker input file unused because linking not done
Finished building: ../src/Visual.cpp

Building target: Visual
Invoking: GCC C++ Linker
g++ -m32  -o "Visual"  ./src/Visual.o   
/usr/bin/ld: ./src/Visual.o: in function main:../src/Visual.cpp:7: error: undefined reference to 'ECL_bDrawrect'
collect2: ld returned 1 exit status
make: *** [Visual] Error 1

**** Build Finished ****

I don't know whether the link is correct or not? How can I link this ecl.o file in properly way?

p/S: I'm using eclipse CDT in ubuntu 11.04 64-bit and the ecl.o is for 32-bit, that's why I have to put -m32 to g++.

7
  • Do you have a declaration of ECL_bDrawrect in "ecl.h"? Did it come from C? Commented Sep 13, 2011 at 9:49
  • yes, it here: void ECL_bDrawrect(int x, int y, int w, int h, int f); Commented Sep 13, 2011 at 9:53
  • WTH is your ecl.o file doing in /usr/include ??? Commented Sep 13, 2011 at 9:56
  • @paxdiablo: i just put it there to make sure it can call from eclipse :D Commented Sep 13, 2011 at 10:00
  • From the new compilation log, you're trying to link at the compile stage and it just spits out "linker input file unused", because it's not linking then. THEN when you get to the linking stage, you're basically just renaming Visual.o to Visual, without linking in ecl.o . Try to move the /usr/include/ecl.o to the linking stage? (I know nothing about eclipse). Commented Sep 13, 2011 at 10:01

2 Answers 2

5

The ecl.o file is not listed on the linker command line.

Also, you probably need to use extern "C" around the include:

extern "C" {
#include "ecl.h"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for quick reply. btw, i did as you said but still get above error.
0

Is it possible that the problem is C++ name mangling? Maybe declaring your functions as extern "C" it will work.

#ifdef __cplusplus 
extern "C" {
#endif
/* ... */
#ifdef __cplusplus
}
#endif

4 Comments

i also got this in the beginning of the ecl.h file:#ifndef __ECL #define __ECL #if defined(__cplusplus) extern "C" { #endif
Where is the declaration of ECL_bDrawrect (not ECL_Drawrect). I think you should enclose it with extern "C" {}, not the macro definition but the actual function declaration.
the declaration of ECL_bDrawrect should be in ecl.o, the problem is I want to use this function by using the ecl.o file.
I'm talking about the prototype. Not the actual function definition. i.e., in some header there should be a declaration like void myfunc(); and in some source there should be a definition like void myfunc() {doSomething();}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.