CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    6

    Question Boost.Extension Tutorial 1 - Problem

    Hi,

    I'm a total beginner to the Boost library and wanted to use Boost.Extension to build a plugin-based program.
    After struggling through the installation of various Boost-stuff I wanted to give tutorial_1 a try:
    http://boost-extension.redshoelace.c...als.tutorial01

    So I created following files:
    shared_test/
    ..|
    ..+- Jamroot
    ..+- tutorial_1/
    .........|
    .........+- hello_world.cpp
    .........+- main.cpp

    Jamroot looks like this:
    ####################################
    import type : change-generated-target-suffix ;
    import type : change-generated-target-prefix ;
    type.change-generated-target-suffix SHARED_LIB : : extension ;
    type.change-generated-target-prefix SHARED_LIB : : lib ;

    import os ;

    local BOOST_ROOT = [ os.environ /usr/local/include/boost ] ;
    project
    : requirements
    <include>#(BOOST_ROOT)
    :
    ;

    lib tutorial_1 : tutorial_1/hello_world.cpp : <link>shared ;
    exe tutorial_1_bin : tutorial_1/main.cpp ;

    install binaries :
    tutorial_1 tutorial_1_bin
    ;
    ####################################

    hello.cpp looks like this:
    ####################################
    #include <iostream>
    #include <boost/extension/extension.hpp>

    extern "C"

    void BOOST_EXTENSION_EXPORT_DECL
    boost_extension_hello_world (int repetitions)
    {
    for (int i = 0; i < repetitions; i++) {
    std::cout << "Hello, World!" << std::endl;
    }
    }
    ####################################

    And main.cpp looks like this:
    ####################################
    #include <iostream>
    #include <boost/extension/shared_library.hpp>
    #include <boost/function.hpp>

    class shared_library;

    int main()
    {
    std::string library_path = "libtutorial_1.extension";
    shared_library lib(library_path);
    if (!lib.open()) {
    std::cerr << "Library failed to open: " << library_path << std << endl;
    return 1;
    }

    boost::function<void (int)>
    f(lib.get<void, int>("boost_extension_hello_world"));

    if (!f) {
    std::cerr << "Function not found!" << std::endl;
    return 1;
    }

    f(4);
    }
    ####################################


    Now the problem: When I'm in the "shared_test"-folder and try to compile the files by typing
    bjam

    I obtain the following error messages:

    gcc.compile.c++ bin/gcc-4.4.3/debug/tutorial_1/main.o
    tutorial_1/main.cpp: In function ‘int main()’:
    tutorial_1/main.cpp:10: error: variable ‘shared_library lib’ has initializer but incomplete type
    tutorial_1/main.cpp:12: error: expected primary-expression before ‘<<’ token
    tutorial_1/main.cpp:12: error: ‘endl’ was not declared in this scope
    tutorial_1/main.cpp:17: error: expected primary-expression before ‘void’
    tutorial_1/main.cpp:17: error: expected primary-expression before ‘int’

    Does anybody see the problem here? I'm sure it's just some kind of newbie problem.

  2. #2
    Join Date
    Sep 2010
    Posts
    6

    Re: Boost.Extension Tutorial 1 - Problem

    Sorry, "hello.cpp looks like this" should be "hello_world.cpp looks like this"...

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Boost.Extension Tutorial 1 - Problem

    Please use code tags when posting code.
    Quote Originally Posted by aloifolia View Post
    Code:
    #include <iostream> 
    #include <boost/extension/shared_library.hpp> 
    #include <boost/function.hpp> 
    
    class shared_library; 
    
    int main() 
    { 
        std::string library_path = "libtutorial_1.extension"; 
        shared_library lib(library_path); 
        //...
    }
    The class shared_library is forward declared here in the global namespace. In main it is instantiated, which is not possible without the class definition. That's what the compiler is telling you.

    Remove the forward declaration. Then you'll probably have to qualify the shared_library class with the boost namespace (as boost::shared_library). Check the boost documentation to be sure.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Sep 2010
    Posts
    6

    Re: Boost.Extension Tutorial 1 - Problem

    Thank you! I should have included the namespaces instead.

    So that's my new main.cpp-file now:
    Code:
    #include <iostream>
    #include <boost/extension/shared_library.hpp>
    #include <boost/function.hpp>
    using namespace boost::extensions;
    
    int main()
    {
        std::string library_path = "libtutorial_1.extension";
        shared_library::shared_library lib(library_path);
        //...
        // "std << endl" was also erroneous
        std::cerr << "Library failed to open: " << library_path << std::endl;    ....
        //...
    }
    But now have a look at the more cryptic error messages:

    gcc.compile.c++ bin/gcc-4.4.3/debug/tutorial_1/main.o
    gcc.link bin/gcc-4.4.3/debug/tutorial_1_bin
    bin/gcc-4.4.3/debug/tutorial_1/main.o: In function `boost::extensions::impl::load_shared_library(char const*)':
    /usr/local/include/boost/extension/impl/library_impl.hpp:60: undefined reference to `dlopen'
    bin/gcc-4.4.3/debug/tutorial_1/main.o: In function `boost::extensions::impl::get_function(void*, char const*)':
    /usr/local/include/boost/extension/impl/library_impl.hpp:64: undefined reference to `dlsym'
    bin/gcc-4.4.3/debug/tutorial_1/main.o: In function `boost::extensions::impl::close_shared_library(void*)':
    /usr/local/include/boost/extension/impl/library_impl.hpp:67: undefined reference to `dlclose'

    Maybe the reason for these errors could arise from the Jamfile?

  5. #5
    Join Date
    Sep 2010
    Posts
    6

    Re: Boost.Extension Tutorial 1 - Problem

    So I just understood that the linker is complaining about not finding the necessary library. I know that -ldl would be the parameter to pass to the compiler. Unfortunately I do not know how to accomplish this in BJam. I think that I'll have a look at the BJam-documentation.

    Anyway I also tried to compile the program by hand. That's what I did:

    1. create dynamic library:
    Code:
    sudo g++ -shared -fPIC hello_world.cpp -o libhello.so
    2. compile the main program:
    Code:
    sudo g++ -o hello main.cpp -ldl
    And then I let it run. Unfortunately loading the dynamic library failed as the program printed "Library failed to open: libhello.so" - that's the string which the main function should print in this case.

    Does anybody have a hint whether it could be because of a misconstruction in the source code or rather be caused by a missing compilation parameter, for instance?

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Boost.Extension Tutorial 1 - Problem

    Quote Originally Posted by aloifolia View Post
    Anyway I also tried to compile the program by hand. That's what I did:

    1. create dynamic library:
    Code:
    sudo g++ -shared -fPIC hello_world.cpp -o libhello.so
    2. compile the main program:
    Code:
    sudo g++ -o hello main.cpp -ldl
    And then I let it run. Unfortunately loading the dynamic library failed as the program printed "Library failed to open: libhello.so" - that's the string which the main function should print in this case.

    Does anybody have a hint whether it could be because of a misconstruction in the source code or rather be caused by a missing compilation parameter, for instance?
    You'll have to copy the library (libhello.so) to the same directory as the program. Normally, you copy everything to a 'bin' directory automatically after a build.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured