Thread: How to Use Libraries in my cpp file?

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...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

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    They put them all in the same directory.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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