Author Topic: How to link a .o file to a project  (Read 30498 times)

Offline greenninja

  • Single posting newcomer
  • *
  • Posts: 3
How to link a .o file to a project
« on: January 24, 2010, 09:09:43 am »
I am trying to add prewritten files which are already compiled to .o files.  I have the header for it but can't seem to figure out how to add the .o file so the linker will recognize it.  I have tried just adding it to the project where you add the source .c or .cpp etc files, and I've tried only including the header, but if I only include the header I don't know where to put the .o file.  Any help would be appreciated.  :)

Thanks
« Last Edit: January 24, 2010, 09:54:19 am by greenninja »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: How to link a .o file to a project
« Reply #1 on: January 24, 2010, 10:31:17 am »
Right click on the object file in the in the "management -> projects" pane, then "Properties -> Build options".
There verify that the "link file" option is checked.

Look here http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F and post the output if you can't handle it yourself

p.s. I've tried it and it doesn't work, it might be bug in C::B.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: How to link a .o file to a project
« Reply #2 on: January 24, 2010, 03:48:19 pm »
You can try to add the object-file to the other linker options of your project, use the path relative to your projects base-path.

Offline greenninja

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to link a .o file to a project
« Reply #3 on: January 24, 2010, 04:23:17 pm »
You can try to add the object-file to the other linker options of your project, use the path relative to your projects base-path.

I am wondering if you can be a little more specific?  I have tried adding that .o file just about everywhere I can but for most of the places I have tried, it doesn't even show up.  So I'm probably just not sure where specifically to look.

I feel like I have gone through all the linker settings that I could possibly find but just keep coming up empty-handed.  When I have the .o file included with the link file option checked as oBFusCATed mentioned, I get the maximum number of errors for "undefined reference to ..."

When I only include the header and not the .o file I get the 3 undefined references to the 3 functions that would call a funtion in that .o file.    Note this is when I have placed a second copy of the .o file in the root folder / obj/debug where the main.o file is placed.  Without it there it doesn't work at all.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: How to link a .o file to a project
« Reply #4 on: January 24, 2010, 04:37:30 pm »
You can try to add the object-file to the other linker options of your project, use the path relative to your projects base-path.

I am wondering if you can be a little more specific?  I have tried adding that .o file just about everywhere I can but for most of the places I have tried, it doesn't even show up.  So I'm probably just not sure where specifically to look.

I feel like I have gone through all the linker settings that I could possibly find but just keep coming up empty-handed.  When I have the .o file included with the link file option checked as oBFusCATed mentioned, I get the maximum number of errors for "undefined reference to ..."

When I only include the header and not the .o file I get the 3 undefined references to the 3 functions that would call a funtion in that .o file.    Note this is when I have placed a second copy of the .o file in the root folder / obj/debug where the main.o file is placed.  Without it there it doesn't work at all.

The header-file is needed in any cases.

Add the object-file to "Build options... -> Linker settings -> Other linker options:"

If you still get undefined references, it might be that the object-file does not include the needed functions, or you have an object-file build with a C-compiler and you use it from within a C++-project or vice versa, or it is build with another (possibly incompatible) compiler.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: How to link a .o file to a project
« Reply #5 on: January 24, 2010, 05:36:29 pm »
Use "nm" command line tool to find what symbols the object file has.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: How to link a .o file to a project
« Reply #6 on: January 24, 2010, 05:38:53 pm »
Use "nm" command line tool to find what symbols the object file has.
Hey - there is the "symbol table" plugin (a wrapper around nm) that does this more nicely and is integrated into C::B. ;-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline greenninja

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to link a .o file to a project
« Reply #7 on: January 24, 2010, 07:32:48 pm »
Well I added the .o file to the linker like Jens suggested, and it seems to be ok, the errors for it no longer assert, but I also have a .a file that needs to be included as well, so now I am trying to compile that into a .o file and include it as well.  I have tried including it similarly to a .cpp or .c file and tell it to link / compile but all attempts i get at this result in an error log similar to this.

mingw32-gcc.exe -Wall -W -g -c "C:\Users\Joshua\Desktop\College\EECS 482\Proj 1 Threads\Threads\Sample Thread\libinterrupt.a" -o obj\Debug\libinterrupt.o
mingw32-gcc.exe: C:\Users\Joshua\Desktop\College\EECS 482\Proj 1 Threads\Threads\Sample Thread\libinterrupt.a: linker input file unused because linking not done
mingw32-g++.exe -o "bin\Debug\Sample Thread.exe" obj\Debug\main.o thread.o obj\Debug\libinterrupt.o
mingw32-g++.exe: obj\Debug\libinterrupt.o: No such file or directory

additionally for the project spec when running this on a linux remote terminal the command line to run this is:

g++ -m32 thread.o main.cc libinterrupt.a -ldl

When I run this with my files on a linux remote terminal it runs perfect, so the .a and .o files are complete, so I am the one messing this up lol.  I have searched the internet for those compile flags and can't find anything.  If anybody has any further suggestions for the .a file issues, I'd greatly appreciate the help.  I thought it was working but apparently the .o file problems were asserting first so I didn't realize it was a problem.

Thanks in advance!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: How to link a .o file to a project
« Reply #8 on: January 25, 2010, 01:15:21 am »
.a file is a static lib (an Ar archive with object files).
Add it in the linker option as any other lib.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]