How to use Libraries











up vote
49
down vote

favorite
51












For some reason I'm never able to use external libraries in any language. I'm looking for instructions/explanations of how to use external libraries, as well as how they work. When I search online, I get fragments that never seem to apply to whatever library I download and try and use. I work on both a mac and a pc, and C++ examples are fine. I use eclipse IDE with the C++ plug in. If there are instructions that apply to all libraries that would be great.










share|improve this question




















  • 1




    When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
    – Jesse Good
    Apr 27 '12 at 22:39










  • Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
    – Benj
    Apr 27 '12 at 22:40






  • 1




    I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
    – sinθ
    Apr 27 '12 at 22:43






  • 3




    Libraries that are do not come as part of a language are typically called "Third Party Libraries"
    – Rob Kielty
    Apr 28 '12 at 0:11










  • Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
    – jww
    May 10 at 2:57















up vote
49
down vote

favorite
51












For some reason I'm never able to use external libraries in any language. I'm looking for instructions/explanations of how to use external libraries, as well as how they work. When I search online, I get fragments that never seem to apply to whatever library I download and try and use. I work on both a mac and a pc, and C++ examples are fine. I use eclipse IDE with the C++ plug in. If there are instructions that apply to all libraries that would be great.










share|improve this question




















  • 1




    When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
    – Jesse Good
    Apr 27 '12 at 22:39










  • Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
    – Benj
    Apr 27 '12 at 22:40






  • 1




    I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
    – sinθ
    Apr 27 '12 at 22:43






  • 3




    Libraries that are do not come as part of a language are typically called "Third Party Libraries"
    – Rob Kielty
    Apr 28 '12 at 0:11










  • Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
    – jww
    May 10 at 2:57













up vote
49
down vote

favorite
51









up vote
49
down vote

favorite
51






51





For some reason I'm never able to use external libraries in any language. I'm looking for instructions/explanations of how to use external libraries, as well as how they work. When I search online, I get fragments that never seem to apply to whatever library I download and try and use. I work on both a mac and a pc, and C++ examples are fine. I use eclipse IDE with the C++ plug in. If there are instructions that apply to all libraries that would be great.










share|improve this question















For some reason I'm never able to use external libraries in any language. I'm looking for instructions/explanations of how to use external libraries, as well as how they work. When I search online, I get fragments that never seem to apply to whatever library I download and try and use. I work on both a mac and a pc, and C++ examples are fine. I use eclipse IDE with the C++ plug in. If there are instructions that apply to all libraries that would be great.







c++ windows macos






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 17 '15 at 0:28









Mogsdad

32.5k1186188




32.5k1186188










asked Apr 27 '12 at 22:33









sinθ

4,2431463108




4,2431463108








  • 1




    When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
    – Jesse Good
    Apr 27 '12 at 22:39










  • Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
    – Benj
    Apr 27 '12 at 22:40






  • 1




    I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
    – sinθ
    Apr 27 '12 at 22:43






  • 3




    Libraries that are do not come as part of a language are typically called "Third Party Libraries"
    – Rob Kielty
    Apr 28 '12 at 0:11










  • Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
    – jww
    May 10 at 2:57














  • 1




    When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
    – Jesse Good
    Apr 27 '12 at 22:39










  • Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
    – Benj
    Apr 27 '12 at 22:40






  • 1




    I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
    – sinθ
    Apr 27 '12 at 22:43






  • 3




    Libraries that are do not come as part of a language are typically called "Third Party Libraries"
    – Rob Kielty
    Apr 28 '12 at 0:11










  • Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
    – jww
    May 10 at 2:57








1




1




When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
– Jesse Good
Apr 27 '12 at 22:39




When you do std::cout << "Hello World"; you are using a library. Could you be more specific?
– Jesse Good
Apr 27 '12 at 22:39












Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
– Benj
Apr 27 '12 at 22:40




Your last question involved #include <iostream> so you've used them to some extent no? You're question is pretty open ended, try something specific and then ask a question about that.
– Benj
Apr 27 '12 at 22:40




1




1




I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
– sinθ
Apr 27 '12 at 22:43




I think my question reveals how little I know of programming. I mean libraries that aren't built in to the language (if that makes any sense).
– sinθ
Apr 27 '12 at 22:43




3




3




Libraries that are do not come as part of a language are typically called "Third Party Libraries"
– Rob Kielty
Apr 28 '12 at 0:11




Libraries that are do not come as part of a language are typically called "Third Party Libraries"
– Rob Kielty
Apr 28 '12 at 0:11












Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
– jww
May 10 at 2:57




Please don't ask for a tutorial. You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."
– jww
May 10 at 2:57












3 Answers
3






active

oldest

votes

















up vote
130
down vote



accepted










Say you have a class Unuseful defined as follows:



File Unuseful.h:



class Unuseful {
public:
void printUnusefulStatement();
};


File Unuseful.cpp:



#include "unuseful.h"
#include <iostream>

void Unuseful::printUnusefulStatement()
{
std::cout << "Hello world!" << std::endl;
}


Now, you have another class that needs printing unuseful statements:



Unuseful u;
u.printUnusefulStatement();


This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.



You may use this library in two ways:




  1. By providing the source code to the compiler

  2. By providing a binary file (which had been previously compiled for your architecture), to the linker


Case 1: using a library at compile time



This is the simplest case.
You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file).
Typically you are the author and user of the library (a class that accomplishes a task you need).



Compiling with this command:



g++ main.cpp unuseful.cpp


allows you to use the implementation you need in your main.cpp file.



Case 2: linking a library



More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).



The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).



Static libraries are created by simply archiving the *.o files with the ar program:



# Create the object files (only one here)
g++ -c unuseful.cpp
# Create the archive (insert the lib prefix)
ar rcs libunuseful.a unuseful.o


Shared libraries are created with the g++ -shared option:



# Create the object file with Position Independent Code[**]
g++ -fPIC -c unuseful.cpp
# Crate the shared library (insert the lib prefix)
g++ -shared -o libunuseful.so unuseful.o


Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.



If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.



It's time to use the library:



g++ main.cpp -L. -lunuseful


The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).



Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).



Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library.
Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:



# Set the LD_LIBRARY_PATH [*]
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.


Done, now your executable has been compiled and it will be able to run and load the library it needs.



Conclusion



This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.



There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.



[*]: In a Unix environment



[**]: If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines. [From the g++ man page]






share|improve this answer



















  • 2




    just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
    – user4858430
    Jun 9 '15 at 19:57


















up vote
6
down vote













Here's where you start
http://en.wikipedia.org/wiki/Library_(computing)



Basically, a 'library' is a collection of compiled functions and class declarations.



On a Mac there are also "frameworks" which are somewhat similar to Pascal's units and contain both the declarations and the compiled code.



In managed languages like Java or C# there are packages and assemblies. Both are closely related to libraries.



To use libraries in C or C++ you've got to have a .lib-file (or .a-file for most POSIX or GCC toolchain based compilers) and the prototypes of the functions which are compiled into the .lib file. Depending on your development environment (for Eclipse you are most likely using the GCC compiler and GNU toolchain with LD linker), you just specify the library files (.lib or .a) as the input to the linker. Most of the time the library is accompanied with header files which contain the definitions of function prototypes.



Even if you did not know about the linker, which is strange enough, the libraries are still used in your program implicitly - the std::cout is in the libstdc++ or the C Run-Time Library.



As an example of a huge library and a useful set of C++ classes you might want to look at Boost.



To write GUI on Windows you can use the WinAPI which is described in MSDN.



To write GUI on Mac you can use Carbon API which is somewhat similar to WinAPI, but is now deprecated. The only way to write "legit" GUI for MacOS is to use Cocoa and Objective-C.



To write cross-platform GUI you can use a lot of libraries: Qt, wxWidgets, GTK among them.



The last, but not the least. C++ is not the best language for GUI.






share|improve this answer



















  • 8




    That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
    – Benj
    Apr 27 '12 at 22:56






  • 2




    OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
    – Viktor Latypov
    Apr 27 '12 at 23:04












  • The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
    – Star Light
    Feb 26 at 6:38


















up vote
0
down vote













The best way to use external C++ libraries is make use of a C++ package manager, go and learn of these;




  • conan

  • vcpkg


  • hunter


  • cppan

  • build2


Some of them involve using CMake, you can find a well written tutorial on it here.



.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10358745%2fhow-to-use-libraries%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    130
    down vote



    accepted










    Say you have a class Unuseful defined as follows:



    File Unuseful.h:



    class Unuseful {
    public:
    void printUnusefulStatement();
    };


    File Unuseful.cpp:



    #include "unuseful.h"
    #include <iostream>

    void Unuseful::printUnusefulStatement()
    {
    std::cout << "Hello world!" << std::endl;
    }


    Now, you have another class that needs printing unuseful statements:



    Unuseful u;
    u.printUnusefulStatement();


    This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.



    You may use this library in two ways:




    1. By providing the source code to the compiler

    2. By providing a binary file (which had been previously compiled for your architecture), to the linker


    Case 1: using a library at compile time



    This is the simplest case.
    You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file).
    Typically you are the author and user of the library (a class that accomplishes a task you need).



    Compiling with this command:



    g++ main.cpp unuseful.cpp


    allows you to use the implementation you need in your main.cpp file.



    Case 2: linking a library



    More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).



    The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).



    Static libraries are created by simply archiving the *.o files with the ar program:



    # Create the object files (only one here)
    g++ -c unuseful.cpp
    # Create the archive (insert the lib prefix)
    ar rcs libunuseful.a unuseful.o


    Shared libraries are created with the g++ -shared option:



    # Create the object file with Position Independent Code[**]
    g++ -fPIC -c unuseful.cpp
    # Crate the shared library (insert the lib prefix)
    g++ -shared -o libunuseful.so unuseful.o


    Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.



    If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.



    It's time to use the library:



    g++ main.cpp -L. -lunuseful


    The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).



    Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).



    Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library.
    Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:



    # Set the LD_LIBRARY_PATH [*]
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.


    Done, now your executable has been compiled and it will be able to run and load the library it needs.



    Conclusion



    This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.



    There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.



    [*]: In a Unix environment



    [**]: If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines. [From the g++ man page]






    share|improve this answer



















    • 2




      just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
      – user4858430
      Jun 9 '15 at 19:57















    up vote
    130
    down vote



    accepted










    Say you have a class Unuseful defined as follows:



    File Unuseful.h:



    class Unuseful {
    public:
    void printUnusefulStatement();
    };


    File Unuseful.cpp:



    #include "unuseful.h"
    #include <iostream>

    void Unuseful::printUnusefulStatement()
    {
    std::cout << "Hello world!" << std::endl;
    }


    Now, you have another class that needs printing unuseful statements:



    Unuseful u;
    u.printUnusefulStatement();


    This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.



    You may use this library in two ways:




    1. By providing the source code to the compiler

    2. By providing a binary file (which had been previously compiled for your architecture), to the linker


    Case 1: using a library at compile time



    This is the simplest case.
    You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file).
    Typically you are the author and user of the library (a class that accomplishes a task you need).



    Compiling with this command:



    g++ main.cpp unuseful.cpp


    allows you to use the implementation you need in your main.cpp file.



    Case 2: linking a library



    More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).



    The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).



    Static libraries are created by simply archiving the *.o files with the ar program:



    # Create the object files (only one here)
    g++ -c unuseful.cpp
    # Create the archive (insert the lib prefix)
    ar rcs libunuseful.a unuseful.o


    Shared libraries are created with the g++ -shared option:



    # Create the object file with Position Independent Code[**]
    g++ -fPIC -c unuseful.cpp
    # Crate the shared library (insert the lib prefix)
    g++ -shared -o libunuseful.so unuseful.o


    Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.



    If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.



    It's time to use the library:



    g++ main.cpp -L. -lunuseful


    The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).



    Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).



    Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library.
    Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:



    # Set the LD_LIBRARY_PATH [*]
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.


    Done, now your executable has been compiled and it will be able to run and load the library it needs.



    Conclusion



    This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.



    There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.



    [*]: In a Unix environment



    [**]: If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines. [From the g++ man page]






    share|improve this answer



















    • 2




      just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
      – user4858430
      Jun 9 '15 at 19:57













    up vote
    130
    down vote



    accepted







    up vote
    130
    down vote



    accepted






    Say you have a class Unuseful defined as follows:



    File Unuseful.h:



    class Unuseful {
    public:
    void printUnusefulStatement();
    };


    File Unuseful.cpp:



    #include "unuseful.h"
    #include <iostream>

    void Unuseful::printUnusefulStatement()
    {
    std::cout << "Hello world!" << std::endl;
    }


    Now, you have another class that needs printing unuseful statements:



    Unuseful u;
    u.printUnusefulStatement();


    This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.



    You may use this library in two ways:




    1. By providing the source code to the compiler

    2. By providing a binary file (which had been previously compiled for your architecture), to the linker


    Case 1: using a library at compile time



    This is the simplest case.
    You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file).
    Typically you are the author and user of the library (a class that accomplishes a task you need).



    Compiling with this command:



    g++ main.cpp unuseful.cpp


    allows you to use the implementation you need in your main.cpp file.



    Case 2: linking a library



    More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).



    The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).



    Static libraries are created by simply archiving the *.o files with the ar program:



    # Create the object files (only one here)
    g++ -c unuseful.cpp
    # Create the archive (insert the lib prefix)
    ar rcs libunuseful.a unuseful.o


    Shared libraries are created with the g++ -shared option:



    # Create the object file with Position Independent Code[**]
    g++ -fPIC -c unuseful.cpp
    # Crate the shared library (insert the lib prefix)
    g++ -shared -o libunuseful.so unuseful.o


    Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.



    If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.



    It's time to use the library:



    g++ main.cpp -L. -lunuseful


    The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).



    Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).



    Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library.
    Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:



    # Set the LD_LIBRARY_PATH [*]
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.


    Done, now your executable has been compiled and it will be able to run and load the library it needs.



    Conclusion



    This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.



    There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.



    [*]: In a Unix environment



    [**]: If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines. [From the g++ man page]






    share|improve this answer














    Say you have a class Unuseful defined as follows:



    File Unuseful.h:



    class Unuseful {
    public:
    void printUnusefulStatement();
    };


    File Unuseful.cpp:



    #include "unuseful.h"
    #include <iostream>

    void Unuseful::printUnusefulStatement()
    {
    std::cout << "Hello world!" << std::endl;
    }


    Now, you have another class that needs printing unuseful statements:



    Unuseful u;
    u.printUnusefulStatement();


    This means that you want to use an external library containing the specific implementation (printUnusefulStatement) that you want to include in your code.



    You may use this library in two ways:




    1. By providing the source code to the compiler

    2. By providing a binary file (which had been previously compiled for your architecture), to the linker


    Case 1: using a library at compile time



    This is the simplest case.
    You have the source code of the library you have to use and you simply have to compile it together with your existing code (say main.cpp file).
    Typically you are the author and user of the library (a class that accomplishes a task you need).



    Compiling with this command:



    g++ main.cpp unuseful.cpp


    allows you to use the implementation you need in your main.cpp file.



    Case 2: linking a library



    More often than Case 1, you don't have the source code of the library you want to use. You only have the header file (Unuseful.h, to continue with the example) and a static or shared library (probably[*] libunuseful.a and libunuseful.so files, respectively).



    The static library is an archive of object files (*.o) that are linked inside your final executables, the shared libraries instead are loaded dynamically - at run time (look at this page for a better understanding of the difference).



    Static libraries are created by simply archiving the *.o files with the ar program:



    # Create the object files (only one here)
    g++ -c unuseful.cpp
    # Create the archive (insert the lib prefix)
    ar rcs libunuseful.a unuseful.o


    Shared libraries are created with the g++ -shared option:



    # Create the object file with Position Independent Code[**]
    g++ -fPIC -c unuseful.cpp
    # Crate the shared library (insert the lib prefix)
    g++ -shared -o libunuseful.so unuseful.o


    Let's suppose now you have the Unuseful.h file and the shared library (libunuseful.so file) and you have a main.cpp file that instantiates a Unuseful object and calls the printUnusefulStatement method.



    If you try to compile this file (g++ main.cpp) the linker will complain because it cannot find the printUnusefulStatement symbol.



    It's time to use the library:



    g++ main.cpp -L. -lunuseful


    The -L option tells the linker where to search for library files and the -l flag tells the linker the name of the libraries to be used (without the lib prefix).



    Now the executable (a.out, because I didn't specify a different name) is created, and you have used a library to implement a functionality you needed (printUnusefulStatement).



    Since the shared library is loaded at run-time, the execution of the a.out executable may fail because the system is not able to find the library.
    Typically this can be solved by appropriately setting an environment variable indicating which paths to use to search for dynamic libraries:



    # Set the LD_LIBRARY_PATH [*]
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.


    Done, now your executable has been compiled and it will be able to run and load the library it needs.



    Conclusion



    This is a rapid overview on libraries which I hope can help you understand how they are used and provided to others.



    There are many many aspects that should be investigated in more detail, if you are interested: g++ options when creating shared libraries, ar options, environment variables, the shared libraries format and so on.



    [*]: In a Unix environment



    [**]: If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines. [From the g++ man page]







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 28 '12 at 11:20

























    answered Apr 27 '12 at 23:01









    Vincenzo Pii

    10.1k62746




    10.1k62746








    • 2




      just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
      – user4858430
      Jun 9 '15 at 19:57














    • 2




      just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
      – user4858430
      Jun 9 '15 at 19:57








    2




    2




    just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
    – user4858430
    Jun 9 '15 at 19:57




    just found this...thanks so much for this clear explanation. I had the overall process but the little details were messing me up
    – user4858430
    Jun 9 '15 at 19:57












    up vote
    6
    down vote













    Here's where you start
    http://en.wikipedia.org/wiki/Library_(computing)



    Basically, a 'library' is a collection of compiled functions and class declarations.



    On a Mac there are also "frameworks" which are somewhat similar to Pascal's units and contain both the declarations and the compiled code.



    In managed languages like Java or C# there are packages and assemblies. Both are closely related to libraries.



    To use libraries in C or C++ you've got to have a .lib-file (or .a-file for most POSIX or GCC toolchain based compilers) and the prototypes of the functions which are compiled into the .lib file. Depending on your development environment (for Eclipse you are most likely using the GCC compiler and GNU toolchain with LD linker), you just specify the library files (.lib or .a) as the input to the linker. Most of the time the library is accompanied with header files which contain the definitions of function prototypes.



    Even if you did not know about the linker, which is strange enough, the libraries are still used in your program implicitly - the std::cout is in the libstdc++ or the C Run-Time Library.



    As an example of a huge library and a useful set of C++ classes you might want to look at Boost.



    To write GUI on Windows you can use the WinAPI which is described in MSDN.



    To write GUI on Mac you can use Carbon API which is somewhat similar to WinAPI, but is now deprecated. The only way to write "legit" GUI for MacOS is to use Cocoa and Objective-C.



    To write cross-platform GUI you can use a lot of libraries: Qt, wxWidgets, GTK among them.



    The last, but not the least. C++ is not the best language for GUI.






    share|improve this answer



















    • 8




      That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
      – Benj
      Apr 27 '12 at 22:56






    • 2




      OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
      – Viktor Latypov
      Apr 27 '12 at 23:04












    • The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
      – Star Light
      Feb 26 at 6:38















    up vote
    6
    down vote













    Here's where you start
    http://en.wikipedia.org/wiki/Library_(computing)



    Basically, a 'library' is a collection of compiled functions and class declarations.



    On a Mac there are also "frameworks" which are somewhat similar to Pascal's units and contain both the declarations and the compiled code.



    In managed languages like Java or C# there are packages and assemblies. Both are closely related to libraries.



    To use libraries in C or C++ you've got to have a .lib-file (or .a-file for most POSIX or GCC toolchain based compilers) and the prototypes of the functions which are compiled into the .lib file. Depending on your development environment (for Eclipse you are most likely using the GCC compiler and GNU toolchain with LD linker), you just specify the library files (.lib or .a) as the input to the linker. Most of the time the library is accompanied with header files which contain the definitions of function prototypes.



    Even if you did not know about the linker, which is strange enough, the libraries are still used in your program implicitly - the std::cout is in the libstdc++ or the C Run-Time Library.



    As an example of a huge library and a useful set of C++ classes you might want to look at Boost.



    To write GUI on Windows you can use the WinAPI which is described in MSDN.



    To write GUI on Mac you can use Carbon API which is somewhat similar to WinAPI, but is now deprecated. The only way to write "legit" GUI for MacOS is to use Cocoa and Objective-C.



    To write cross-platform GUI you can use a lot of libraries: Qt, wxWidgets, GTK among them.



    The last, but not the least. C++ is not the best language for GUI.






    share|improve this answer



















    • 8




      That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
      – Benj
      Apr 27 '12 at 22:56






    • 2




      OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
      – Viktor Latypov
      Apr 27 '12 at 23:04












    • The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
      – Star Light
      Feb 26 at 6:38













    up vote
    6
    down vote










    up vote
    6
    down vote









    Here's where you start
    http://en.wikipedia.org/wiki/Library_(computing)



    Basically, a 'library' is a collection of compiled functions and class declarations.



    On a Mac there are also "frameworks" which are somewhat similar to Pascal's units and contain both the declarations and the compiled code.



    In managed languages like Java or C# there are packages and assemblies. Both are closely related to libraries.



    To use libraries in C or C++ you've got to have a .lib-file (or .a-file for most POSIX or GCC toolchain based compilers) and the prototypes of the functions which are compiled into the .lib file. Depending on your development environment (for Eclipse you are most likely using the GCC compiler and GNU toolchain with LD linker), you just specify the library files (.lib or .a) as the input to the linker. Most of the time the library is accompanied with header files which contain the definitions of function prototypes.



    Even if you did not know about the linker, which is strange enough, the libraries are still used in your program implicitly - the std::cout is in the libstdc++ or the C Run-Time Library.



    As an example of a huge library and a useful set of C++ classes you might want to look at Boost.



    To write GUI on Windows you can use the WinAPI which is described in MSDN.



    To write GUI on Mac you can use Carbon API which is somewhat similar to WinAPI, but is now deprecated. The only way to write "legit" GUI for MacOS is to use Cocoa and Objective-C.



    To write cross-platform GUI you can use a lot of libraries: Qt, wxWidgets, GTK among them.



    The last, but not the least. C++ is not the best language for GUI.






    share|improve this answer














    Here's where you start
    http://en.wikipedia.org/wiki/Library_(computing)



    Basically, a 'library' is a collection of compiled functions and class declarations.



    On a Mac there are also "frameworks" which are somewhat similar to Pascal's units and contain both the declarations and the compiled code.



    In managed languages like Java or C# there are packages and assemblies. Both are closely related to libraries.



    To use libraries in C or C++ you've got to have a .lib-file (or .a-file for most POSIX or GCC toolchain based compilers) and the prototypes of the functions which are compiled into the .lib file. Depending on your development environment (for Eclipse you are most likely using the GCC compiler and GNU toolchain with LD linker), you just specify the library files (.lib or .a) as the input to the linker. Most of the time the library is accompanied with header files which contain the definitions of function prototypes.



    Even if you did not know about the linker, which is strange enough, the libraries are still used in your program implicitly - the std::cout is in the libstdc++ or the C Run-Time Library.



    As an example of a huge library and a useful set of C++ classes you might want to look at Boost.



    To write GUI on Windows you can use the WinAPI which is described in MSDN.



    To write GUI on Mac you can use Carbon API which is somewhat similar to WinAPI, but is now deprecated. The only way to write "legit" GUI for MacOS is to use Cocoa and Objective-C.



    To write cross-platform GUI you can use a lot of libraries: Qt, wxWidgets, GTK among them.



    The last, but not the least. C++ is not the best language for GUI.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 16 '17 at 19:23









    Majid

    773619




    773619










    answered Apr 27 '12 at 22:50









    Viktor Latypov

    12.3k32746




    12.3k32746








    • 8




      That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
      – Benj
      Apr 27 '12 at 22:56






    • 2




      OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
      – Viktor Latypov
      Apr 27 '12 at 23:04












    • The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
      – Star Light
      Feb 26 at 6:38














    • 8




      That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
      – Benj
      Apr 27 '12 at 22:56






    • 2




      OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
      – Viktor Latypov
      Apr 27 '12 at 23:04












    • The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
      – Star Light
      Feb 26 at 6:38








    8




    8




    That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
    – Benj
    Apr 27 '12 at 22:56




    That last point is a bit debatable, best != easiest, alot of serious UIs are still written in C++. All the browsers are for example, including Chrome which is new and had pleanty of alternatives when it was designed.
    – Benj
    Apr 27 '12 at 22:56




    2




    2




    OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
    – Viktor Latypov
    Apr 27 '12 at 23:04






    OK. I agree. It all depends. When portability comes to mind, not much to think about but C/C++. However, the front-end may be implemented in a platform-specific manner and C++ can be used for all of the business logic.
    – Viktor Latypov
    Apr 27 '12 at 23:04














    The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
    – Star Light
    Feb 26 at 6:38




    The last one you said as if we could use another language to write GUI and C++ is only used for core? How come?
    – Star Light
    Feb 26 at 6:38










    up vote
    0
    down vote













    The best way to use external C++ libraries is make use of a C++ package manager, go and learn of these;




    • conan

    • vcpkg


    • hunter


    • cppan

    • build2


    Some of them involve using CMake, you can find a well written tutorial on it here.



    .






    share|improve this answer

























      up vote
      0
      down vote













      The best way to use external C++ libraries is make use of a C++ package manager, go and learn of these;




      • conan

      • vcpkg


      • hunter


      • cppan

      • build2


      Some of them involve using CMake, you can find a well written tutorial on it here.



      .






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The best way to use external C++ libraries is make use of a C++ package manager, go and learn of these;




        • conan

        • vcpkg


        • hunter


        • cppan

        • build2


        Some of them involve using CMake, you can find a well written tutorial on it here.



        .






        share|improve this answer












        The best way to use external C++ libraries is make use of a C++ package manager, go and learn of these;




        • conan

        • vcpkg


        • hunter


        • cppan

        • build2


        Some of them involve using CMake, you can find a well written tutorial on it here.



        .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 10:42









        adem

        12319




        12319






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10358745%2fhow-to-use-libraries%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff