Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How to use C++ external libraries

Options
  • 19-05-2011 12:57pm
    #1
    Registered Users Posts: 324 ✭✭


    Hi there,

    Relatively new to c++. I need to use this modbus library (or another if you can recommend) to ping a meter connected via a serial connection. I am unsure how to go about using it however. I have used libraries with java, but I am unsure how to include them with C++ in eclipse. I tried adding the location to the linker settings under the C/C++ Build settings, but I get an error "No such file or directory" referring to a file (port.h ....its in the library, just in a different location to other files).

    Should I be including only specific folders or the entire directory. Is this the correct way to go about it? Do I need #includes in the code?

    Thanks for any help, I could really use some!
    Tagged:


Comments

  • Registered Users Posts: 4,764 ✭✭✭cython


    greyed wrote: »
    Hi there,

    Relatively new to c++. I need to use this modbus library (or another if you can recommend) to ping a meter connected via a serial connection. I am unsure how to go about using it however. I have used libraries with java, but I am unsure how to include them with C++ in eclipse. I tried adding the location to the linker settings under the C/C++ Build settings, but I get an error "No such file or directory" referring to a file (port.h ....its in the library, just in a different location to other files).

    Should I be including only specific folders or the entire directory. Is this the correct way to go about it? Do I need #includes in the code?

    Thanks for any help, I could really use some!

    Ok, you seem to have gotten at least some way towards linking the library. This port.h should be in a directory on your compiler/IDE's include path, and you should have a statement like
    #include <port.h>
    
    at the top of the file where you are using the library.

    The next steps depend a bit on how you want to link the library exactly (statically/dynamically), and potentially the OS and compiler combination you are using, so could you please clarify that?


  • Registered Users Posts: 324 ✭✭greyed


    cython wrote: »
    Ok, you seem to have gotten at least some way towards linking the library. This port.h should be in a directory on your compiler/IDE's include path, and you should have a statement like
    #include <port.h>
    
    at the top of the file where you are using the library.

    The next steps depend a bit on how you want to link the library exactly (statically/dynamically), and potentially the OS and compiler combination you are using, so could you please clarify that?

    Thanks for your reply. I could not tell you if I need a static or dynamic link tbh, could you explain a little further? I am using windows 7 professional 32bit, and the minGW gcc compiler.

    My main problem seems to be that I am unsure which componants of the folder I need. I added the entire thing, and now my problem lies not with port.h, but with FreeRTOS.h ...its in a demo file for a different operating system, and I have no idea of its function :/ Should I remove any files or are libraries generally in a workable structure as-is?


  • Registered Users Posts: 4,764 ✭✭✭cython


    greyed wrote: »
    Thanks for your reply. I could not tell you if I need a static or dynamic link tbh, could you explain a little further? I am using windows 7 professional 32bit, and the minGW gcc compiler.

    My main problem seems to be that I am unsure which componants of the folder I need. I added the entire thing, and now my problem lies not with port.h, but with FreeRTOS.h ...its in a demo file for a different operating system, and I have no idea of its function :/ Should I remove any files or are libraries generally in a workable structure as-is?

    Ok, first, have a read here for the difference between static and dynamic linking. Hopefully that will help you decide which you need (or which you have been provided with libraries for).

    Secondly, when you say you "added the entire thing", what does the "entire thing" consist of, and how did you add it? I ask this because there are different stages in building a C++ program, and different parts of a library are used in different stages:
    1. Preprocessor - processes all statements beginning with #. Header files are included here
    2. Compilation of code - compiles your code to object files, and machine instructions
    3. Linking - takes the above machine instructions/code, and links them, and any external static libraries together, outputting a functional program.

    Dynamically linked libraries are not sought until runtime, and are useful if more than one program may need access to the same functionality.

    Basically you should have your header files in a folder that is either on an include path (unlikely in Windows), or that the compiler/IDE has been told to look in (usually configured in the project properties. Your (static) libraries should then be in a folder that is on the linker path (again unlikely for you), or that the the linkers knows to look in (similar in set up to the include folders). Finally, you also need to tell your linker (usually through project properties) what static libraries it needs to use, out of all the ones that it may have access to. I am not familiar enough with eclipse to tell you where to set all this up, but google may be able to aid you, once you know the steps involved.


  • Registered Users Posts: 324 ✭✭greyed


    This is the content of the folder i downloaded and unzipped.

    UOj6S.png

    and then in the "modbus" folder...

    RuybC.png

    "includes" contains many .h files, "functions" has some .c files and ascii, the form of modbus I wish to use has two files, a header and a source file for mbascii.

    "demo", in the first image, contains many example files, one for various operating systems. Which brings me to another question. The windows demo.cpp is C++, while the other files that make up the lib are .c. Is it written in both? Could you tell me why this is?

    By "entire thing" I mean all the contents shown above :P I added it by entering the location in the "library search path" in eclipse, in the linker "libraries" section. This may not be correct at all. Would my libraries be the .c files in "functions" in the sub folder shown above? and the header files you are talking about - the contents of "include"?

    Does this library look conventional to you? Its a bit daunting trying to find one that will work and is well documented.

    Sorry for the big post :P im fairly confused...


  • Registered Users Posts: 981 ✭✭✭fasty


    Don't bother with library search paths. Can you add the h and c files to your project? That's the way the Windows Visual Studio demo does it.


  • Advertisement
  • Registered Users Posts: 324 ✭✭greyed


    I am not sure, but i'll try. There are many classes though, both .c and .cpp. Im not sure how to integrate them all, which (not) to include, etc :P


Advertisement