Thread: How to Use Libraries in my cpp file?

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    How to Use Libraries in my cpp file?

    Friends, I've looked at every conceivable link on the net to explain how to use a 3rd party library in my code. I really have spent the past 3 days trying to do this simple task and I see many newbies on the internet have the same question.

    To try and guide you all, for the purposes of this exercise, let us use:

    1) Code::Blocks as the IDE as its probably one you are most verse in.
    2) "Boost" library as its one you are most verse in.

    Using this common IDE and common library, I have done the following, all VERBATIM as per advise on the one FAQ page:

    Observe carefully my precise steps:

    1) I downloaded the official windows Boost.zip library (Version 1.54.0) and extracted it into the following folder on my computer: C:\boost_1_54_0

    2) I opened CodeBlocks, created a new project and main cpp source file.

    3) I tell the compiler where to find headers and library files: I set up global compiler path directories for the boost library by opening settings -> compiler and debugger -> tab search directories -> sub tab compiler -> add button and entering the search directory: C:\boost_1_54_0

    4) I tell the linker where to find headers and library files: I set up global linker path directories for the boost library by opening settings -> compiler and debugger -> tab search directories -> sub tab compiler -> add button and entering the search directory: C:\boost_1_54_0

    5) I tell the linker which libraries the programme is using: I right click my project on the left hand side pane and select build options. I click the linker tab. Under Linker Settings I press add and then I add the library I wish to use: C:\boost_1_54_0\

    6) In my main source code, I add the header:
    Code:
    #include <boost\algorithm\string\find.hpp>
    as this is the function header I wish to use. You see, its a header not a library, as Boost contains header only files and there is no need to build.




    Anyway, after doing all this I get the error that the programme exited with status 1 and the exe couldnt even run. All I had was a main function with no contents just to check if it runs. Whats worse, after setting all these options, the programme doesnt even recognise the string variable even after including #include <string>. It says string not declared in this scope.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post your code that is causing the problems, and the complete error messages exactly as they appear in your development environment.

    Did you properly scope the functions you're trying to use?

    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Post the error or warnings you are getting!

    Post the full Compiler Build Log. This is NOT the message Log!

    FAQ-Compiling (errors) - CodeBlocks

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Hi Guys, My code is:

    Code:
    #include <boost\algorithm\string\find.hpp
    
    int main () {
    return 0;
    }

    As I said above, no core code, I just put a minimal programme to see if it will work.

    Any way when I compile and run that I get the message:
    "It seems this project has not been built yet"
    Do you want to build it now?"
    I click yes

    After compiling and running, I get a red highlighted message under Build Log:
    collect 2: Id returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    0 errors, 0 warnings

  5. #5
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    It seems as though since I added all those path directories and added the libraries, any programme that I compile and run gives the same error.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Post the error or warnings you are getting!

    Post the full Compiler Build Log. This is NOT the message Log!

    FAQ-Compiling (errors) - CodeBlocks

    Tim S.
    Do a re-build and post the Full Build Log!
    Until you Post a good Full Build Log I will ignore you!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That is an include only library, AFAIK.

    You have to enable C++11.
    Your example compiles without problems for me, only when I add -std=c++11, no boost related linker options are needed.

  8. #8
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by stahta01 View Post
    Do a re-build and post the Full Build Log!
    Until you Post a good Full Build Log I will ignore you!

    Tim S.
    I set the options so that it outputs a complete build log and also to export to HTML for full report. The following is all I get:

    -------------- Build: Debug in Test ---------------

    mingw32-g++.exe -LC:\boost_1_54_0\ -o bin\Debug\Test.exe obj\Debug\Untitled2.o C:\boost_1_54_0\
    c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../../mingw32/bin/ld.exe: C:\boost_1_54_0\: No such file: No such file or directory
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    0 errors, 0 warnings
    Build log saved as:
    file://C:%5cUsers%5cMyName%5cDocuments%5cC%20Code%5cTest% 5cTest_build_log.html

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My first suggestion is that you undo all the changes you made to your compiler configuration. Then try a basic program to insure your compiler works:
    Code:
    int main()
    {
    
       return 0;
    }
    If this compiles, then try including the file using the complete path (c:/complete/path_to_include_file/include_file_name).
    Code:
    #include < C:\boost_1_54_0\.... >
    
    int main()
    {
    
       return 0;
    }
    By the way I don't see any -I compiler directive in your compile line. The -I informs the compiler where to look for additional include files.

    Also the following is one of the keys to your problem:
    C:\boost_1_54_0\: No such file: No such file or directory
    Jim
    Last edited by jimblumberg; 09-24-2013 at 07:59 AM.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read this link FAQ-Compiling (general) - CodeBlocks

    It is strongly suggested to NOT change the Global Compiler Settings; but, instead change the project settings!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Jim, I redid the entire sequence in the opening post but this time I left out step 5.

    This time when I execute the same code:
    Code:
    #include <boost\algorithm\string\replace.hpp
     
    int main () {
    
    return 0;
    }
    It finally works and returns 0 without any errors or warnings.

    But now I want to go a step further. I actually want to use the replace function.

    So I do:
    Code:
    #include <string>
    #include <boost\algorithm\string\replace.hpp
    
    using namespace std;
    
    int main (){
    
    stirng str1 = "Hello Dolly, Hello World!"; replace_first(str1, "Dolly", "Jane"); return 0;
    }
    Although this time the compiler detects the library, I get an error:
    error: 'replace_first' was not declared in this scope

  12. #12
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Stahta, I also changed the directories in the poject and not global variables but to no avail

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    stirng
    should be
    Code:
    string
    And I would use
    Code:
    std::string
    and get rid of using namespace...

    If you just typed your post - copy past your code instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by vart View Post
    If you just typed your post - copy past your code instead
    That the result of testing the code on a computer without internet and a laptop with no testing facilities and internet :-)

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You probably need to scope the "replace" function. Open the include file and see what namespace that function is encased within.

    Also you can add the boost directory to your project by adding the proper directory in your build settings section

    How to Use Libraries in my cpp file?-projectsettings3-jpg


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Libraries correctly specified in make file
    By mapleleafblue in forum C Programming
    Replies: 0
    Last Post: 09-04-2009, 08:54 AM
  2. C++ libraries
    By Poincare in forum C++ Programming
    Replies: 8
    Last Post: 08-16-2009, 04:46 PM
  3. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  4. Libraries
    By punkrockguy318 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2004, 08:52 PM
  5. .so libraries
    By Skarr in forum Linux Programming
    Replies: 4
    Last Post: 11-10-2002, 01:35 AM