I find myself doing a lot of library including and juggling in recent projects with code::blocks- and I'm looking to improve my efficiency.
Currently, since I'm doing work with curl, zlib, wxwidgets, and other libraries, it takes quite some time to append the list after the initial project creation (even if I used a template).
Here's quick peek at a working project with all of the library files and how I'm including them:
Now to the two part question:
How can I quickly add several library files at once, in the correct order? (the order is evidently quite important)
How do .a files translate into "other linker options", in other words, can I simply include the search directory and use something like lwxmsw32u_core instead of including ...>\libwxmsw32u_core.a?
Alright, so there's a simple solution, the project file for code::blocks is quite mundane, a light XML implementation- you can very easily copy libraries (in the correct order) from one project to another by editing the plain text of the .cbp file.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
This may seem a bit ridiculous to ask, but I'm struggling to find a good system for this. Are there any standardized systems for storing and organizing third party development libraries? I'm plagued with OCD and consider myself to be a "neat-freak," so I tend to use tons of directories/subdirectories for organizing items, but since I'm a newer developer, I just recently started dabbling with 3rd party libraries and I'm not sure how to go about organizing them. I would use a ton of folders and make a huge hierarchy, but doing that may result in some extremely long absolute paths in the #includes of my source code. Any suggestions?
Third party libraries can go anywhere you want, as long as you make sure that both the compiler and the compiled program can find the files that they need. So you can go wild with organizing your files in whatever you want, as long as you make sure that you tell this to the compiler and the compiled program.
Exactly how to do this depends on the IDE (integrated development environment) that you are using. I use visual studio, so the exact terminology might be different if you use something else. In visual studio, you have to go to the properties of your project to do this.
For the header files of your external libraries, go to Configuration Properties > C/C++ > General and click on "Additional Include Directories". If you edit the value of this field, you can add the paths to the include directory of your external library, which should hold all the header files of the external library. By using the Macros that Visual Studio provides, you can make these paths relative, so that you don't have to do this all over whenever you move your project. Make sure you don't remove "%(AdditionalIncludeDirectories)". Also make sure that the selected configuration and platform at the top of the window match the configuration and platform for which you are trying to compile. Using the macros you can set this up for all the configurations and platforms simultaneously, which is a bit harder but it will save you time in the long run. An example from one of my projects:
$(SolutionDir)dependencies\SDL2_image-2.0.1\include;$(SolutionDir)dependencies\SDL2-2.0.5\include;%(AdditionalIncludeDirectories)
For the library files, you need to go to Configuration Properties > Linker > General and click on "Additional Library Directories". Edit this value to add the paths to the lib folders of your external libraries. You can again use macros here.
$(SolutionDir)dependencies\SDL2-2.0.5\lib\x64;$(SolutionDir)dependencies\SDL2_image-2.0.1\lib\x64;%(AdditionalLibraryDirectories)
Next, go to Configuration Properties > Linker > Input and edit "Additional Dependencies" to add the names of the .lib files that you need. Just the filenames this time, you don't need the path here. An example from one of my projects:
SDL2main.lib;SDL2.lib;SDL2_image.lib;%(AdditionalDependencies)
Finally, you need to make sure that your compiled program can find the .dll files of your external libraries. For this you can mess around with system variables like PATH and so on, but I don't recommend that. I prefer to use a Post-Build Event. An event like this is basically a sequence of command line commands that are carried out after your program has been compiled. You can add this event by going to Configuration Properties > Build Events > Post-Build Event and editing "Command Line". An example of what you can put here, from one of my projects, is shown below:
copy /Y "$(SolutionDir)dependencies\SDL2_image-2.0.1\lib\$(PlatformTarget)\*.dll" "$(TargetDir)*.dll"
copy /Y "$(SolutionDir)dependencies\SDL2-2.0.5\lib\$(PlatformTarget)\SDL2.dll" "$(TargetDir)SDL2.dll"
xcopy /Y /S /E /I "$(SolutionDir)assets" "$(TargetDir)assets"
Note that I use macros again: $(SolutionDir), $(PlatformTarget), $(TargetDir) are replaced by the solution directory, platform target and the target directory respectively for each combination of Configuration and Platform.
You have 2 options:
Use system-installed libraries (and headers), tell compiler where to find them.
Use a folder in your project (use folder ./external to store external sources, e.g. ./external/boost-asio for Boost ASIO library), download sources when initializing the project (I prefer to use git submodules to download external sources), and build them with your project.
CMAKE is a build tool which can help you in achieving both.
Either way, as jtbandes wrote, don't use absolute paths.
I have an object file library that exists as a standalone VC++ solution. I have a number of other completely separate VC++ solutions, and I would like some of them to utilise the classes included in this library.
However, since they are in the same solution, I cannot seem to add them as a dependency. I have attempted to investigate so-called "linker" dependencies but can't get it to work.
Does anyone know of a standard, modern efficient way to do this. Eventually my plan is to conglomerate these projects into a single solution as I believe they should be, but that is not something I have the time to deal with right now.
I think you are mixing terms project and solution. Generally speaking, solution is a collection of projects, with specified dependencies between them.
Library should be a project (possibly dependent on another projects), but not a solution. If you would like to include your library in another solutions, simplest way to do that would be to add project for library (and any dependent projects) to solutions you would like to add them by right clicking on solution->add->add existing project->add your project. This will ensure library will be compiled as part of solution.
You would need to set dependencies between projects (in your solution), and add include/linker path for the library to any projects using it within the solution.
I'm normally working in c# so certain things in c++ keep confusing me alot (they seem so diffrent yet the names almost the same)
I created a Console project in which i want to run a diffrent project for testing purposes. i added the project as a reference to the console app, and then got kinda stuck.
there is no namespace in the projects, so i can't do a using and if i try to include the other file, it cannot find it (and i want to avoid being unable to debug through it all).
the code for the class can be found here(ignore the c# part), the console is just a standard console with nothing in it yet.
Yeah, C++ doesn't have the notion of assemblies that exists in C# and .NET. It makes tasks like this slightly more difficult, a virtue of the fact that C++ compiles directly to native code.
Instead, you'll generally #include the necessary header files (*.h) at the top of your code file, and instruct the linker to link to the appropriate .lib file(s). Do that by going to your project's Properties, selecting Linker -> Input, and adding the file to the "Additional Dependencies" section.
As an alternative to linking to the .lib file, you can use Visual Studio to add a reference to the other project, if it's part of the same solution. Microsoft has a walk-through on creating and using a dynamic link library in C++ that might be worth a read.
I'll assume you're using Visual Studios:-). You have to tell
the compiler where to look for its includes. Under Visual
Studios, open the properties page for the project, then go to
Configuration Properties->C/C++->General, and add the necessary
directories in the entry Additional Include Directories. (If
the other project is in the same solution, use a relative path.
But I think the dialog box that pops up when you click on the
button on the right does this automatically. I'm not a great
fan of all this GUI stuff in general, but Microsoft seems to
have done this particular part quite well.)
Once you've done this, you might have to go through a similar
process for linking: this time it's under Configuration
Properties->Linker->General, and the entry is called Additional
Library Directories, but the principle is the same. (This may
not be necessary, if you're putting all of the dll's and
executables in the project in the same directory.)
I am right now reorganizing my project and what recently was a simple application now became a pair of C++ projects - static library and real application.
I would like to share one precompiled header between two projects, but face some troubles with setting up the .pdb file paths.
Assume my first project is called Library and builds it's .lib file with a corresponding Library.pdb file. Now, the second project is called Application and builds everything into the same folder (.exe and another Application.pdb file).
Right now my both projects create their own precompiled headers file (Library.pch and Application.pch) based on one actual header file. It works, but I think it's a waste of time and I also think there should be a way to share one precompiled header between two projects.
If in my Application project I try to set the Use Precompiled Header (/Yu) option and set it to Library.pch, it wouldn't work, because of the following error:
error C2858: command-line option 'program database name "Application.pdb" inconsistent with precompiled header, which used "Library.pdb".
So, does anyone know some trick or way to share one precompiled header between two projects preserving proper debug information?
The question is, why do you want to share the precompiled header (PCH) files. Generally I woul d say, that does not make sense. PCH are used to speed up compiling not to share any information between different projects.
Since you also write about the PDB file, you probably want to debug the library code with your applications. This can be achieved by setting the /Fd parameter when compiling the library. When you link the library in your application and the linker finds the corresponding PDB file, you get full debug support.
This sounds complicated and cumbersome to set up. More than that, it may not be possible at all.
Instead, you can include the precompiled header from one application into the second. It will still be compiled once for the second project, but maintenance becomes easy and you do not have to redefine the dependencies in the second project (just include them).
I am having a bit of trouble organising my source files.
I have my own small, but growing collection of code that I would like to use in various projects. The file and folder layout is something like this:
library\sub1\source.h
library\sub1\source.cpp
library\sub2\source.h
library\sub2\source.cpp
One of my problems is that I want to include this code, as needed, in my other projects. To date I have used absolute paths to point to the libary code, but there must be a better way.
Futhermore, I need to add every library file I use to a project's files Visual Studio in order for it to compile correctly.
So my question in short is how do I fix this? What is the proper/best way to handle the above situation.
You shouldn't, in general, add source files from libraries directly to other projects. Compile them separatly as a library and use those.
For organising the library's directory structure itself, right now I settled on something like the following structure
library1/widget.h
library1/private/onlyinlib.h
library1/private/widget.cpp
(and if applicable)
library1/private/resources/widget.jpg
library1/private/project/widget.xcode
I put all headers directly in the library path, and have a subfolder private which will contain everything that's only used by the library, but should never be shared / exposed.
The greatest advantage is that every project I start only needs a include path pointing at the directory containing my libraries, then every (public) include is done like
#include "library1/widget.h"
private includes are simply
#include "onlyinlib.h"
This has a number of advantages:
If new libraries are introduced, there's no messing with project /compiler settings to get the headers 'visible'.
Moving to other compilers / platforms is also very little hassle.
The headers are automatically 'namespaced', i.e. by including part of the path too, it's next to impossible to get a nameclash with the includes
It's immediatly obvious where a header comes from, and if a header is part of the public interface or not
I don't think that there's a proper way to do this - it's going to depend on exactly what you are trying to achieve.
Here's some things you might not be aware of:
You can use relative paths in your projects.
You can use environment variables in paths.
You can add directories to Visual Studio's search rules.
This gives you a little more control over where you put the include files and if you add your folders to Visual Studio's search rules you don't have to include any paths at all.
If you must include third-party code instead of just linking with a pre-compiled version (e.g., perhaps you need to make modifications or tweaks to it), consider branching it in whatever you use for source-control:
/trunk/... --- your code goes here
/thirdparty --- pristine copies of third-party libraries go here
/thirdparty/lib1
/thirdparty/lib2
etc.
/trunk/lib1 --- branched from: /thirdparty/lib1, perhaps with local changes
this is the version that you build/link with.
Assuming you use a decent source-control system, this scheme will allow you to easily upgrade to newer versions of third-party libraries and then merge those changes with the changes you've made locally.
For example, suppose "lib1" releases a new version:
Commit the change to /thirdparty/lib1.
Merge from /thirdparty/lib1 to /trunk/lib1
Fix any merge conflicts.
This is, IMO, the only sane way to handle upgrading third-party libraries to which you've made local modifications.
First: Add all used directorys to your project include paths. Add them as relative paths if possible.
Second: You must add all used librarys/source files to your project. This can be either done in the project explorer, or in the Project->Linker tab. In the latter case, you'll have to add the used directories to the projects library paths as well.
Usually its not a good idea to use paths in #include directives.