« Close

Datasheets and User Guides

App Notes

Software & Driver

 

C/C++ for LJM - Windows, Mac, Linux

LJM C/C++ Examples Windows/Mac/Linux (Software Platform)

C/C++ examples for the LJM library. Also contains some header files:

  • LJM_Utilities.h - contains useful functions
  • stream/LJM_StreamUtilities.h - contains useful stream functions
  • LabJackMModbusMap.h - contains declarations for register addresses, names, and types
Release
01/11/2017
Beta
02/01/2017

LJM C/C++ Examples (Referenceable)

Prerequisites

Note: The Mac OS X and Linux downloads already include these examples.

    Instructions

    1. Download/extract the .zip file attached at the top of this page.
    2. Navigate to the extracted LabJackM folder.
    3. Open and run .c/.cpp source files using your code editor of choice.

    Code Snippet

    #include <stdio.h>
    #include <LabJackM.h>
    
    #include "LJM_Utilities.h"
    // Available in the C/C++ examples download. Provides the ErrorCheck function.
    
    int main()
    {
        int err, handle;
        double value = 0;
    
        const char * NAME = {"SERIAL_NUMBER"};
    
        // Open first found LabJack
        err = LJM_Open(LJM_dtANY, LJM_ctANY, "LJM_idANY", &handle);
        ErrorCheck(err, "LJM_Open");
    
        // Call LJM_eReadName to read the serial number from the LabJack.
        err = LJM_eReadName(handle, NAME, &value);
        ErrorCheck(err, "LJM_eReadName");
    
        printf("eReadName result:\n");
        printf("    %s = %f\n", NAME, value);
    
        // Close device handle
        err = LJM_Close(handle);
        ErrorCheck(err, "LJM_Close");
    
        return LJME_NOERROR;
    }

    8 comments

    I am new to Visual Studio and am so far unable to link any programs provided from the examples. Can you provide some direction on how exactly

    to reference the header files and libraries in Visual Studio?

    The examples downloads on this page contain example .sln and .vcproj files in the visual_studio_2008 folder, so you could look at those files for Visual Studio 9 examples.

    More generally, here are some Visual Studio Configuration Properties that are necessary for LJM.

    C/C++:

    • Additional Include Directories: $(ProgramFiles)\LabJack\Drivers
    • Calling Convention: __cdecl (/Gd)

    Linker:

    • Additional Library Directories: $(ProgramFiles)\LabJack\Drivers
    • Additional Dependencies: LabJackM.lib

    Looks like an issue with linking to the LJM library. Just for a quick test, you could try opening one of the projects/solutions in the download's visual_studio_2008 folder and see if they compile.

    As for the errors you are getting, make sure your linker settings look like:

    Linker:

    • Additional Library Directories: $(ProgramFiles)\LabJack\Drivers
    • Additional Dependencies: LabJackM.lib

    If you are compiling a 64-bit application, change the "Additional Library Directories" to "$(ProgramFiles)\LabJack\Drivers\64bit" instead. If the proper LabJackM.lib file isn't used for linking the compiler will not find the LJM functions/symbols.

    The examples in the visual_studio_2008 folder compile with no issues.  My linker settings are identical to those above.  I am attempting to compile for Win32.

    I would recommend going through Microsoft's LNK2019 error guide, if you haven't already:

    https://msdn.microsoft.com/en-us/library/799kze2z.aspx

    In particular, it may be helpful to use the /VERBOSE flag to make sure LabJackM.lib is being linked. Also, the stackoverflow it mentions may be generally helpful to you.

    You can also try adding LabJackM.lib to your project or otherwise ensuring that LabJackM.lib is where you think it is.

    SOLUTION:

    In the Visual Studio 2013 project properties dialog, change C/C++ > Preprocessor > Preprocessor Definitions

    from the default:  _MBCS;%(PreprocessorDefinitions)

    to:    WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)

    The example code snippet will now compile and run.

    Note:  Add the statement WaitForUserIfWindows(); just above the return LJME_NOERROR; statement to keep the console window open for inspection after the program has finished execution.

    Thanks for posting this!

    Also, WaitForUserIfWindows is defined in LJM_Utilities.h, for anyone who might be looking at this in the future.

    I can compile the code snippet above if I paste it into the .c file of an existing Visual Studio example project, so my problem must be due to incorrect settings somewhere...