Like Tree4Likes

How to Use Libraries in my cpp file?

This is a discussion on How to Use Libraries in my cpp file? within the C++ Programming forums, part of the General Programming Boards category; This code worked for me. Code: #include <string> #include <boost\algorithm\string\replace.hpp> using std::string; using boost::algorithm::replace_first; int main () { string str1 ...

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    This code worked for me.

    Code:
    #include <string>
    #include <boost\algorithm\string\replace.hpp>
    
    using std::string;
    using boost::algorithm::replace_first;
    
    int main ()
    {
       string str1 = "Hello Dolly, Hello World!";
       replace_first(str1, "Dolly", "Jane");
       return 0;
    }
    Note: As always I am a C programmer learning C++; so, I could easily be doing something wrong.

    Note: I have been told many times that using "using namespace" is a bad thing that textbooks and beginners do; and, to stop doing that.


    Tim S.
    Vespasian likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  2. #17
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by stahta01 View Post
    This code worked for me.

    Code:
    #include <string>
    #include <boost\algorithm\string\replace.hpp>
    
    using std::string;
    using boost::algorithm::replace_first;
    
    int main ()
    {
       string str1 = "Hello Dolly, Hello World!";
       replace_first(str1, "Dolly", "Jane");
       return 0;
    }
    Note: As always I am a C programmer learning C++; so, I could easily be doing something wrong.

    Note: I have been told many times that using "using namespace" is a bad thing that textbooks and beginners do; and, to stop doing that.


    Tim S.
    FINALLY IT WORKS!! THANKS TIM!!!

    Ha, okay, ceteris paribus, and after making a few changes the code worked. I also included cout using std::cout and not through the using std namespace. It was a pleasure falling victim to using std namespace as the trap was experienced through practice and not through reading theory and forum rants.

    I dont know why it works, but it works. Two things though:
    1) Does this mean that everytime I use third party libraries I cant use using std namespace?
    2) how different would this process be if I had to use compiled libraries? i.e. with this Boost algorithm, one only need use linking to header files and not obj. files

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Vespasian
    1) Does this mean that everytime I use third party libraries I cant use using std namespace?
    You can, if you qualify every name for which there could be a conflict, or if you know that the unqualified names that are affected by the using directive will not result in conflicts (e.g., you only use the using directive in a limited scope).

    Quote Originally Posted by Vespasian
    2) how different would this process be if I had to use compiled libraries? i.e. with this Boost algorithm, one only need use linking to header files and not obj. files
    Rather than "linking to header files", we would usually say "including of header files". As for the difference: from your first post in this thread it seems like you already understand that you may have to link the library files when necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The problems I see from your first attempt are:
    - You specified a path instead of the specific library to link with. There is include path, library path and there is the libraries to link with. The last one must be the filenames of the libraries to link with and the linker will look for them in the library path(s).
    - You failed to account for that replace lies in the boost::algorithms namespace. You either have to use a namespace-directive or fully qualify the namespace.

    Btw, the above code can also be written as

    Code:
    #include <string>
    #include <boost\algorithm\string\replace.hpp>
     
    namespace balg = boost::algorithm::replace_first;
     
    int main ()
    {
       std::string str1 = "Hello Dolly, Hello World!";
       balg::replace_first(str1, "Dolly", "Jane");
       return 0;
    }
    ...which I much prefer. If the namespace is too long, use a namespace alias instead of importing it into the global namespace.
    stahta01 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Apologies for digging out old skeletons here, but a similar problem has arisen:

    I have downloaded SQLite3 which contains sqlite3.def and sqlite.dll for the latest library version.

    How do I link a dll to my main programme?

    Tried everything, including the closest I could get:

    Code:
    #include <stdio.h>
    #include <sqlite3.h>
    
    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int rc;
    
       rc = sqlite3_open("test.db", &db);
    
       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stderr, "Opened database successfully\n");
       }
       sqlite3_close(db);
    }
    I did the same thing as before; I set the search directories of the compiler and linker to the location of the dll and def file.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    For SQLite, it is easiest to use the amalgamation, i.e., just add the huge source file (the amalgamation of all the original source files of SQLite) to your project, or otherwise compile and link it, and include the header where you need to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by laserlight View Post
    For SQLite, it is easiest to use the amalgamation, i.e., just add the huge source file (the amalgamation of all the original source files of SQLite) to your project, or otherwise compile and link it, and include the header where you need to.
    I did this to the above code and got the error: "sqlite3.h: No such file or directory"

    It cant find:
    sqlite.dll
    even though I set the search directory of the compiler and linker to it, namely:
    C:\ThisProject\bin\Debug
    (note how the dll is also in the same directory as the exe just in case

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Vespasian
    I did this to the above code and got the error: "sqlite3.h: No such file or directory"
    That just means that you need to add sqlite3.h to say, the same directory as your other header files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by laserlight View Post
    That just means that you need to add sqlite3.h to say, the same directory as your other header files.
    Im a bit confused. I only have one source file, namely my main file posted above. I dont have other header files? Im not an expert at this linking stuff so I might need lamens terms

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    They put them all in the same directory.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 01: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, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21