User forums > Using Code::Blocks

Can't link object file from another project

(1/4) > >>

Aleksandr:
Seems like at link step C::B generate incorrect path to .o files which manually added to the project from another one.

Create two empty projects a and b. Add two files to the a:


--- Code: (foo.h) ---void bar();
--- End code ---


--- Code: (foo.c) ---#include <stdlib.h>
#include "foo.h"

void bar()
{
  return;
}
--- End code ---

Compile Debug and Release. By default output dirs for me is [Debug|Release]/obj/
Well we have foo.o in both. Next add this .o files to the project b either Project->Add files... or right click on title in tree widget. Of course one for Debug another for Release.

And single file to the b:


--- Code: (main.c) ---#include "../a/foo.h"

int main()
{
  bar();
  return 0;
}
--- End code ---

So, I get this output when compiling b:


--- Code: ---i686-w64-mingw32-gcc.exe -g -pedantic -Wall -IC:\Code\MinGW\include -c C:\Users\Aleksandr\Code\C++\b\main.c -o Debug\obj\b\main.o
i686-w64-mingw32-g++.exe -LC:\Code\MinGW\lib -o Debug\b.exe Debug\obj\a\Debug\obj\foo.o Debug\obj\b\main.o   
i686-w64-mingw32-g++.exe: error: Debug\obj\a\Debug\obj\foo.o: No such file or directory

--- End code ---

For foo.o was added unnecessary prefix Debug\obj\ from Project->Properties->Build targets->Objects output dir as if it was compiled in project b. But I think using relative or absolute path in this case is correct.

scarphin:
Afaik 'add files' is for adding source files not linker or lib files. And I'm not sure about your intention for trying to add a '.o' file to your project?

ollydbg:

--- Quote from: scarphin on June 15, 2014, 01:08:49 am ---Afaik 'add files' is for adding source files not linker or lib files. And I'm not sure about your intention for trying to add a '.o' file to your project?


--- End quote ---
I totally agree with you. Should add libs not .o files.

Aleksandr:

--- Quote from: scarphin on June 15, 2014, 01:08:49 am ---Afaik 'add files' is for adding source files not linker or lib files.

--- End quote ---
Resource (.rc, .res) files can also be added to the project and they appear in the category "Resources" and not "Others", despite the fact that .res is already compiled and only need to link. But while it's ok with .rc, with .res is the same as with the .o file.


--- Quote from: scarphin on June 15, 2014, 01:08:49 am ---And I'm not sure about your intention for trying to add a '.o' file to your project?

--- End quote ---
The main reason for this is that the dialogue "Add Files..." uses the default mask "All files (*.*)", While the mask "Only supported types" does not exist. Which makes it possible to consider adding these files possible. The same happens in the dialog "Add files recursively...".


--- Quote from: ollydbg on June 15, 2014, 04:41:40 am ---I totally agree with you. Should add libs not .o files.

--- End quote ---
I replaced object (.o) file with library (.a) file, but the problem remained. C::B when linking still adds to the path an unnecessary prefix instead use a relative or absolute path.

Although your answers relate to "should" or "should not" add object files to the project, the question a little bit of something else.

I come to the conclusion that if there is a file in the project which is set in the properties "Link" and not "Build", then, regardless of where it is located in the file system, at the beginning of its path will prefix taken from project's properties from "Objects output dir". Is this correct?

scarphin:

--- Quote from: eMan.Lived on June 15, 2014, 12:11:35 pm ---
--- Quote from: scarphin on June 15, 2014, 01:08:49 am ---And I'm not sure about your intention for trying to add a '.o' file to your project?

--- End quote ---
The main reason for this is that the dialogue "Add Files..." uses the default mask "All files (*.*)", While the mask "Only supported types" does not exist. Which makes it possible to consider adding these files possible. The same happens in the dialog "Add files recursively...".


--- Quote from: ollydbg on June 15, 2014, 04:41:40 am ---I totally agree with you. Should add libs not .o files.

--- End quote ---
I replaced object (.o) file with library (.a) file, but the problem remained. C::B when linking still adds to the path an unnecessary prefix instead use a relative or absolute path.

--- End quote ---
Those issues might be bugs in CB but still adding an object file '.o' by 'add files' dialogue doesn't make sense. If you are trying to add a library file or such, you should use the linker settings to accomplish that. Or else AGAIN what is your intention in trying to add an '.o' file to a project?

Navigation

[0] Message Index

[#] Next page

Go to full version