4 Replies - 400 Views - Last Post: 18 June 2019 - 06:07 PM Rate Topic: -----

#1 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 20-January 16

How to include/link custom libraries in other folders ?

Posted 12 June 2019 - 08:36 PM

Hello,


I'm trying to develop libraries in C++ and include them in the main project.

But I don't want to develop the libraries in the same project folder. But I want to develop them in other external folders.

I tried different approaches but I don't even know which library type I should develop in the first place, but I chose static library. And tried to add .a file but still having a problem in building the complete test project.

I have both the static lib and the main project files in separate folders in the same main folder. Like:


Main project folder, contains:
1. static library test project folder
2. main application project folder

My IDE is codeblocks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to include/link custom libraries in other folders ?

#2 jimblumberg   User is offline

  • member icon

Reputation: 5733
  • View blog
  • Posts: 17,568
  • Joined: 25-December 09

Re: How to include/link custom libraries in other folders ?

Posted 13 June 2019 - 05:55 AM

Quote

But I don't want to develop the libraries in the same project folder.

Normally you would keep all of your library sources in separate directories. But you may want to combine your actual compiled library into a common library directory to make using them easier. Also your libraries header files should be separated from the C/C++ source files as well as your library source files being separated from the compiled library. If all the pieces are in related separated directories it makes using the libraries easier.

I use a directory structure something in the line of the following:

Working (usually is somewhere in your "home" directory.
   Utility
      lib  // Contains the compiled libraries.
      include  // Contains the header files for the different libraries.
   
      source // Contains the source files for the different libraries.
         library1  // Of course you would use a descriptive name for your libraries. (This contains the project files for the libraries.
            source // contains the actual source files for the library.
         library2
            source


Quote

I tried different approaches but I don't even know which library type I should develop in the first place, but I chose static library.

IMO, static libraries are easier to use and develop, once you get the basics down you can then compile the same library for both static and dynamic use much easier.

Quote

And tried to add .a file but still having a problem in building the complete test project.


How did you try adding the .a file to your project?

How did you #include the library header file?


Jim
Was This Post Helpful? 1
  • +
  • -

#3 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 20-January 16

Re: How to include/link custom libraries in other folders ?

Posted 16 June 2019 - 12:50 AM

View Postjimblumberg, on 13 June 2019 - 05:55 AM, said:

Normally you would keep all of your library sources in separate directories.


Yep, that's what I want to do :)

Quote

But you may want to combine your actual compiled library into a common library directory to make using them easier.
How to do that?

First of all, what you mean by "actual compiled library"? Do you mean that any library I develop to use in a project application?

If so, then what is "a common library directory"? Do you mean, that I have all my C++ libraries in a specific directory main folder that contains all the source, header and application files? Yes I have that.


Quote

Also your libraries header files should be separated from the C/C++ source files as well as your library source files being separated from the compiled library. If all the pieces are in related separated directories it makes using the libraries easier.


Yep, it's an important point. First, when I started to develop .c libraries for my Arduino AVR/GCC coding, I tend to put all the .c/.h files in one folder. It worked OK for quite a time, then I wanted to improve my set of files and libraries.

For my learning, I stopped my embedded programming and focused on desktop programming to learn how to develop good C/C++ libraries.

Now, I succeeded to link shared/static libraries in codeblocks, I would consider dynamic library for next setup.

Here is my codeblcoks settings as the most basic needed for codeblocks to compiler and run the project with the linked libraries.

1. Shared libraries:
	1. Search directories/Compiler tab, include the path to the folder of the library
	Ex: F:\computer_programming_dev\Programming_exercises\C++\classes\class_project1\shared_lib1
	
	2. Search directories/Linker tab, include the path to the release folder of the library
	Ex: F:\computer_programming_dev\Programming_exercises\C++\classes\class_project1\shared_lib1\bin\Release
	
	3. Linker settings, include the .a file
	Ex: F:\computer_programming_dev\Programming_exercises\C++\classes\class_project1\shared_lib1\bin\Release\libshared_lib1.a

// related problems of setting up the project
1. error: no such file or directory -> missing path for library folder
2. undefined reference to function  -> missing .a file								
3. System Error						-> missing .dll file in project application bin folder
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. Dynamic libraries:
	Same as shared libraries
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3. Static libraries:
	Search directories/Compiler tab, just include library path




Quote

I use a directory structure something in the line of the following:

Working (usually is somewhere in your "home" directory.
   Utility
      lib  // Contains the compiled libraries.
      include  // Contains the header files for the different libraries.
   
      source // Contains the source files for the different libraries.
         library1  // Of course you would use a descriptive name for your libraries. (This contains the project files for the libraries.
            source // contains the actual source files for the library.
         library2
            source


I didn't quite understand this structure! Here's my understanding for your structure:

1. "Utility" is the main folder that contains all libraries.
2. "lib" contains the .dll files, I hope I'm correct.
3. "include" contain .h files for all the libraries in "Utility" folder.
4. "source" contain all .c/.cpp files for the libraries in "include" folder.
5. Didn't get the next "library1", "library2" and their source files. are they in the same "Utility" folder? Why they are not in the main "include" and "source" folders?

Quote

IMO, static libraries are easier to use and develop, once you get the basics down you can then compile the same library for both static and dynamic use much easier.


I of course don't know much about the differences between the 3 types of libraries, I first learned how to set them up.

Static libraries are easier and I think they are the basic type of libraries. Shared/Dynamic have I guess more advantages in running bigger applications.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5733
  • View blog
  • Posts: 17,568
  • Joined: 25-December 09

Re: How to include/link custom libraries in other folders ?

Posted 16 June 2019 - 10:07 AM

Quote

First of all, what you mean by "actual compiled library"?


In this case I mean any library that you have compiled and want to use in some other project.

Quote

If so, then what is "a common library directory"?


Some directory of your choice.

Quote

Do you mean, that I have all my C++ libraries in a specific directory main folder that contains all the source, header and application files? Yes I have that.


This is again your choice.

I normally have a directory in my "Projects" folder that contains only the binary libraries and in another directory that contains only the #include files that are necessary for someone to use that library. Any #include files that are only required to build the library are contained in a sub directory of the directory that contains the library sources. IMO, this makes it easier to "copy" the libraries to another computer to be used by others. But how you lay out your files is your choice, unless you need to follow some institutions layout.

Quote

1. "Utility" is the main folder that contains all libraries.

This directory will contain the libraries (usually links to the directory where the actual library was created) in the bin directory. The #include directory contains links to only the #include files needed to use the various libraries.

Quote

2. "lib" contains the .dll files, I hope I'm correct.

see 1. above.

Quote

3. "include" contain .h files for all the libraries in "Utility" folder.

See 1 above.

Quote

4. "source" contain all .c/.cpp files for the libraries in "include" folder.

The "source" directory (quite often in various different "base" directories), contains the source code required to build the various libraries, in this case I used the imaginative library names of library1, library2, etc. These "source" directories usually have several different sub directories like source, include, bin (which usually has several different sub directories), etc. The "base" directory of each library usually contains the various project and support files for the particular project.

Quote

5. Didn't get the next "library1", "library2" and their source files. are they in the same "Utility" folder? Why they are not in the main "include" and "source" folders?

Because each "library" has it's own source files. In my main Utility directory there are only links to the various files, those links are in "Source" directories.

Again the idea is to keep the "building" of the libraries separate from the "using" of the libraries. Oh you should consider having something like a "git" repository for each of your various projects to track changes and allow recovery of "old" libraries.

Quote

Static libraries are easier and I think they are the basic type of libraries.

Static libraries are linked directly into the programs that use them. Dynamic libraries are common libraries used by different programs at run time, their advantages are that the program that uses them generally tend to be smaller, the disadvantages are that the library (.so on Linux, .dll on Windows) must be in a directory accessible to the program that needs the library. The major problem ist that it is possible to have multiple versions of the same library at the same time.


Quote

Here is my codeblcoks settings as the most basic needed for codeblocks to compiler and run the project with the linked libraries.

How did you add your libraries to the project (you are using a project correct?)? Normally you should use the project/options method of adding the various libraries to your build.



Jim
Was This Post Helpful? 1
  • +
  • -

#5 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 20-January 16

Re: How to include/link custom libraries in other folders ?

Posted 18 June 2019 - 06:07 PM

View Postjimblumberg, on 16 June 2019 - 10:07 AM, said:

In this case I mean any library that you have compiled and want to use in some other project.


OK, I have been working on setting up the basic linking of the 3 types of libraries. Shared/dynamic and static, I learned how to set them up in their original folders.

The next step is the method you told me about which is to put .h files in one folder, .cpp/.c files in another folder. That's ok I understand that.

But, before I do that, each library in its original library, if I moved the header/source files, then what to do with the libraries' other important files; which are, .cbp, .a, .o, .dll, .layout, .def, ... etc? That's the most important point that I need to learn now because I know if I moved the files arbitrary, things won't work as I would expect.


Quote

I normally have a directory in my "Projects" folder that contains only the binary libraries and in another directory that contains only the #include files that are necessary for someone to use that library. Any #include files that are only required to build the library are contained in a sub directory of the directory that contains the library sources. IMO, this makes it easier to "copy" the libraries to another computer to be used by others. But how you lay out your files is your choice, unless you need to follow some institutions layout.


For example, how about the following folders setup:

I develop one main folder for the other sub-folders; but, this structure I thought about from the structure you told me about + the Mingw setup.
The sub-folders are:
1. header files folder: that contain all .h files; like, "include" folder in Mingw directory
2. source: for .c/.cpp files, but this one doesn't exist in Mingw directory set.
3. binary: for .dll and .exe
4. lib: for .o, .a, files

But the question still like the previous one of how to link them together?


Quote

The "source" directory (quite often in various different "base" directories), contains the source code required to build the various libraries, in this case I used the imaginative library names of library1, library2, etc. These "source" directories usually have several different sub directories like source, include, bin (which usually has several different sub directories), etc. The "base" directory of each library usually contains the various project and support files for the particular project.


Yep, this seem to be a bit wild to me, but I went through the directories of the Mingw installation directory and displayed the folders arrangement, I noticed that there are main folders for bin, include, lib, libexec .. etc. And inside these main folders there are folders that contain other files.

But anyway I now should just learn how to setup libraries folders for my desktop C++/embedded C/C++ programming.


Quote

Again the idea is to keep the "building" of the libraries separate from the "using" of the libraries.

Yeah I know this seem to be a main goal or aspect of setting up the files in specific folders. But because I have no much experience of this, so I'm working to finish this part of files arrangement to get to programming stuff.


Quote

Oh you should consider having something like a "git" repository for each of your various projects to track changes and allow recovery of "old" libraries.
I think this would be an advanced things, but absolutely I would be happy to get a grasp on that.

Quote

Static libraries are linked directly into the programs that use them.

Absolutely! I have to include them in the project tree + their path in build options.

Quote

Dynamic libraries are common libraries used by different programs at run time, their advantages are that the program that uses them generally tend to be smaller, the disadvantages are that the library (.so on Linux, .dll on Windows) must be in a directory accessible to the program that needs the library.


I really don't have a much experience in programming world. So considering the Dynamic libraries features, I would say, I think it's typical of how Dynamic libraries work and how to link them to programs.

So, I like the advantage of it, but the disadvantage that the .dll file "must be in a directory accessible to the program that needs the library.", why it's a disadvantage?

Quote

The major problem ist that it is possible to have multiple versions of the same library at the same time.
Why there could be "multiple versions of the same library at the same time"? Is it a compilation problem that when I compile the library with a different setup, that would develop a new version of the library. Or maybe it's not a compilation problem ... hmm, I don't have anything in my mind right now about something that's not a compilation related!


Quote

How did you add your libraries to the project (you are using a project correct?)? Normally you should use the project/options method of adding the various libraries to your build.


Yep, I'm using a console application as the main project, then I developed the other types of libraries; shared, dynamic and static. In the same folder that contain the main project and I'm just practicing of working with setting/linking the libraries to the main project.

My next step is to separate the files to sub-main folders and try to link them.

Then I would compare this method with having each library in its original folder for my embedded programming.


Thanks Jim for the support .. I really appreciate your help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1