Sign in to follow this  

Using a library (how to add .cpp files to a project)

This topic is 2663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone.
Suppose we have a library which consist of two files: A.h and A.cpp.
In A.h there is a class declaration in A.cpp class definition.
These two files are in directory that do not belong to the project.
I want to use this library. So in my project i set "Additional include directory" to the directory where files are and include A.h in my source files.
But how can i make compiler to compile A.cpp file? I don't want to add it to my project. When i use boost or mfc i just include a header and it works.But there are no .cpp files in the project. How compiler finds the .cpp files and compile them?
Thanks.

Share this post


Link to post
Share on other sites
Significant parts of Boost are header-only. Other parts (like boost::filesystem) need to be compiled before but they are (usually) built into a large set of static libraries. On compilers which support it, Boost headers will automatically request linking those static libraries into the project (for example #pragma comment(lib, "whatever.lib") in MSVC).
This of course means you must have created the static library before (in case of boost either because you ran make/bjam or downloaded a precompiled distribution) and the library file must be available in one of the library search paths of your linker.

Share this post


Link to post
Share on other sites
Well it compiles the CPP files added into your project, what I think your looking for is make your own LIB file. This is a compiled cpp file(s) which you can include in other projects by including the header and adding a reference to the lib in the appropriate linker options.

To create a lib file you can include as an additional dependency, create a "static library" project. Add the cpp file(s) to it and compile it. Instead of getting a EXE file you get a .LIB that you can place in whatever directory you would like to keep your libs in, set "Additional library directory (whatever its called)", add it as you would other .lib files.

If like me, you are lazy, use Visual C, and don't want to have to manually add the lib to the linker options on each project you can include the following code in A.h

#pragma comment(lib, "a.lib")

Which does the job for you.

Share this post


Link to post
Share on other sites

This topic is 2663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this