Author Topic: Can't link object file from another project  (Read 26807 times)

Offline Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Can't link object file from another project
« on: May 10, 2014, 03:09:19 pm »
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();

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

void bar()
{
  return;
}

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;
}

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

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.
[AAAA|+}

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Can't link object file from another project
« Reply #1 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?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Can't link object file from another project
« Reply #2 on: June 15, 2014, 04:41:40 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?

I totally agree with you. Should add libs not .o files.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Can't link object file from another project
« Reply #3 on: June 15, 2014, 12:11:35 pm »
Afaik 'add files' is for adding source files not linker or lib files.
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.

And I'm not sure about your intention for trying to add a '.o' file to your project?
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...".

I totally agree with you. Should add libs not .o files.
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?
[AAAA|+}

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Can't link object file from another project
« Reply #4 on: June 15, 2014, 04:36:36 pm »
And I'm not sure about your intention for trying to add a '.o' file to your project?
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...".

I totally agree with you. Should add libs not .o files.
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.
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?

Offline Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Can't link object file from another project
« Reply #5 on: June 15, 2014, 06:28:40 pm »
If you are trying to add a library file or such, you should use the linker settings to accomplish that.
I am not trying, C::B allows me to do it. And yes, I know it and now I'm using this method.

Or else AGAIN what is your intention in trying to add an '.o' file to a project?
Well, I'll try to explain it clearly.

First, in the Project build optionsLinker settings file list is titled Link libraries. From this it is not clear (at least to me) that here you can also add the object files or compiled windows resource files.

Secondly, in the PropertiesBuild for each file, you can uncheck the Compile file leaving checked only Link file.
Hence, I concluded that the project may contain files that do not require compiling, but used when linking. I do not know how it is for you, but for me it is primarily the files .o, .a and .res.

Thirdly, about why I add the .o file to the project. It is smaller than the entire lib. It does not require compiling when building the project.

And finally, what would you advise if I have added to the project .res file, and not an .o file? Since when it is added to the project, the symptoms are the same.
[AAAA|+}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Can't link object file from another project
« Reply #6 on: June 17, 2014, 02:05:00 am »
And finally, what would you advise if I have added to the project .res file, and not an .o file? Since when it is added to the project, the symptoms are the same.
RES files should work fine. If they don't please post a build log, exact steps to reproduce or even an example project.

About the object file: Probably it is a bug, but you'll have to wait for someone familiar with the build system to answer.

@Morten can you confirm this is bug, expected behaviour or something that a user should not do?

BTW: There are two workarounds:
1. Paste the relative path in the linker options
2. Use a static library. I don't know why do you think it is bigger in size, but it is not or at least in is not by much. A static library is just an ar archive containing object files and an index.
« Last Edit: June 17, 2014, 02:07:14 am by oBFusCATed »
(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: Can't link object file from another project
« Reply #7 on: June 17, 2014, 07:23:13 am »
About the object file: Probably it is a bug, but you'll have to wait for someone familiar with the build system to answer.
@Morten can you confirm this is bug, expected behaviour or something that a user should not do?
Yes, its a bug. What <you can do as another workaround is to put it to the additional linker options.

However, I agree with oBFusCATed that using a static library for such purposes is way more convenient.
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 Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Can't link object file from another project
« Reply #8 on: June 17, 2014, 06:43:51 pm »
RES files should work fine. If they don't please post a build log, exact steps to reproduce or even an example project.

Like in the first post, I created the empty project a. In the project PropertiesBuild targetsType I selected GUI application. Added main.cpp file:

Code: main.cpp
#include <windows.h>
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
return 0;
}

In the project folder I created a subfolder res\. Copied the resource.res there (you can create and build the project "Win32 GUI project" to get this one). Added this file to the project. Tried to build the project:

Code: "build log"
i686-w64-mingw32-g++.exe -Wall -O2 -IC:\MinGW\include -c C:\Users\Aleksandr\Code\C++\a\main.cpp -o release\obj\main.o
i686-w64-mingw32-g++.exe -LC:\MinGW\lib -o release\a.exe release\obj\main.o release\obj\res\resource.res  -static -s   -mwindows
i686-w64-mingw32-g++.exe: error: release\obj\res\resource.res: No such file or directory
Process terminated with status 1 (0 minute(s), 4 second(s))
1 error(s), 0 warning(s) (0 minute(s), 4 second(s))

As you can see the same problem.

2. Use a static library. I don't know why do you think it is bigger in size, but it is not or at least in is not by much. A static library is just an ar archive containing object files and an index.

Maybe it's the fact that I was not entirely clear how the linking static libraries.
Now, I believe that in the resulting file will have all of the code is included in the library, ie all "archived" objects. Even those that are not used or needed. Correct me if I'm wrong.
[AAAA|+}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Can't link object file from another project
« Reply #9 on: June 17, 2014, 08:55:43 pm »
Correct me if I'm wrong.
You're wrong 50%. I'm not sure what is the default behaviour, but linkers can remove unused functions and code these days.
(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 Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Can't link object file from another project
« Reply #10 on: June 17, 2014, 10:11:21 pm »
I am also inclined to think that modern linkers have the possibility of such optimization. Well, or at least should have. But the use of the object file gives me 100% certainty. ;)
[AAAA|+}

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Can't link object file from another project
« Reply #11 on: June 17, 2014, 10:44:19 pm »
Why don't you just add the corresponding source files from the library sources to your project? Then you will have only the needed part of your library in your project.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Can't link object file from another project
« Reply #12 on: June 18, 2014, 08:41:19 am »
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 Aleksandr

  • Multiple posting newcomer
  • *
  • Posts: 21
Re: Can't link object file from another project
« Reply #13 on: June 18, 2014, 06:28:56 pm »
Why don't you just add the corresponding source files from the library sources to your project? Then you will have only the needed part of your library in your project.

As I wrote before, it does not need to compile. Why do the same job twice?
Well, your proposal is just another way. You can come up with some exotic such as symlinks or something more sophisticated. But ask a question. Why when linking, you think some objects as a given, and others as wrong? Are they bad or guilty? ;-)

Fixed in trunk.

Does this mean that now you can add libraries to the project in two separate places? Will the confusion in this case?
[AAAA|+}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Can't link object file from another project
« Reply #14 on: June 18, 2014, 08:45:57 pm »
In fact this is interesting proposal. Add a library to a project and C::B will automatically link it:)
But no it is not implemented.

Morten has just fixed the feature where you add res/obj file and it just works!
You can add the object file manually in the other linker options, but I don't see how this is a problem or how this is confusing.
No one is forcing you to put anything there, but if you need to pass something special to the linker you can do it.
(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!]