Using CANlib Visual Studio 2017 C++

  • November 20, 2018
  • Lars-Göran Fredriksson

In this document I will show you how to enable CANlib in Visual Studio 2017 when creating a C++ project. I will show how to create WIN32 and WIN64 applications. I will also show how to handle the platform-settings: x86 and x64. When this is done, you can use the same project (source code) for creating WIN32 and WIN64 applications without editing your code. The instructions in this document can also be used when enabling CANlib in an existing project.


Abbreviations

VS2017Microsoft Visual Studio 2017 (C++)

CANlib Kvaser CANlib SDK www.kvaser.com/download


Before we start ...

First we must download and install “Kvaser CANlib SDK“ and also “Kvaser Drivers for Windows”. When CANlib is installed, please check your hard-drive and identify where Kvaser CANlib has been installed.

On my 64-bit Windows machine it is installed here:

c:\Program Files (x86)\Kvaser\Canlib\INC\canlib.h

c:\Program Files (x86)\Kvaser\Canlib\Lib\MS\canlib32.lib

c:\Program Files (x86)\Kvaser\Canlib\Lib\x64\canlib32.lib

Please remember where you found them as you will need this information soon.

Now we are ready to start Visual Studio 2017 (C++).

(I have used “Microsoft Visual Studio Professional 2017” when creating this text.)


Creating an empty project

Creating a project in VS2017 can be done in many different ways; this is one way to do it.

Select “File” – “New” -”Project…”

Screen Shot 2018-11-13 at 7.01.52 PM

I chose to create a Windows Console App, Visual C++

Edit the name and the rest of unique information and press “OK”.

Screen Shot 2018-11-13 at 7.02.55 PM

Now VS2017 creates an application for us.

It is a bit boring, only an empty console that does almost nothing.

Screen Shot 2018-11-13 at 7.03.50 PM

Adding some code that uses CANlib (C++)

I will now add some code that uses CANlib. Exactly what it does is not important in this example. For more information about how to use CANlib, please check CANlib SDK Help.

Add some code inside the “main()”.

int main()
{
	canStatus R;
	int number_of_channels;

	printf("%s\n", "HelloWorld C++ Builder (c)2018 Kvaser AB");

	canInitializeLibrary();

	unsigned int V0 = canGetVersionEx(canVERSION_CANLIB32_PRODVER32);

	printf("%s %d.%d\n", "Found CANLib version", (V0 & 0xFF0000) >> 16, (V0 & 0xFF00) >> 8);

	R = canGetNumberOfChannels(&number_of_channels);
	printf("%s %d %s\n", "Found", number_of_channels, "channels");

	R = canUnloadLibrary();	printf("If you can read this, everything worked! (Press any key)");
	getchar();
	return 0;
}

When trying to run the application, I get multiple errors. Something is wrong and VS2017 cannot find CANlib.

Screen Shot 2018-11-13 at 7.09.39 PM

This is correct behavior for this application as we need to add information so VS2017 can find CANlib. See section 5: ‘Add CANlib to project’. 


Add CANlib to project

We need to do five things to be able to build our project:

  1. Add the LIB name “canlib32.lib”
  2. Add path to INC (…\Kvaser\Canlib\INC\canlib.h)
  3. Add path to LIB (x86) (…\Kvaser\Canlib\Lib\MS\canlib32.lib)
  4. Add path to LIB (x64) (…\Kvaser\Canlib\Lib\x64\canlib32.lib)
  5. Add #include “canlib.h”

Add the LIB name “canlib32.lib”
Open menu:

Project > Properties >  Configuration properties > Linker > Input > Additional Dependencies

  • Select Configuration: ”All Configurations”
  • Select Platform: “All Platforms”

Add the text “canlib32.lib”. You only need to type it, no browsing.

Screen Shot 2018-11-13 at 7.14.01 PM

Result:

Screen Shot 2018-11-13 at 7.15.05 PM

Add path to INC
Open menu:

Project > Properties > Configuration properties > C++ > General > Additional Include Directories

  • Select Configuration: ”All Configurations”
  • Select Platform: “All Platforms”

Edit “Additional Include Directories”

Screen Shot 2018-11-13 at 7.16.25 PM

Add :”c:\Program Files (x86)\Kvaser\Canlib\INC”

Screen Shot 2018-11-13 at 7.17.28 PM

Result:

Screen Shot 2018-11-13 at 7.18.28 PM

Add path to LIB (x86)
Open menu:

Project > Properties >  Configuration properties > Linker > General-> Additional Library Directories

  • Select Configuration: ”All Configurations”
  • Select Platform: “Win32”

Edit “Additional Library Directories”

Screen Shot 2018-11-13 at 7.21.32 PM

Add “C:\Program Files (x86)\Kvaser\Canlib\Lib\MS”

Screen Shot 2018-11-13 at 7.22.36 PM

Result:

Screen Shot 2018-11-13 at 7.23.41 PM

Add path to LIB (x64)
Open menu:
Project > Properties >  Configuration properties > Linker > General-> Additional Library Directories

  • Select Configuration: ”All Configurations”
  • Select Platform: “Win64”

Edit “Additional Library Directories”

Screen Shot 2018-11-13 at 7.23.41 PM

Add “C:\Program Files (x86)\Kvaser\Canlib\Lib\x64”

Screen Shot 2018-11-13 at 7.28.35 PM

Result:

Screen Shot 2018-11-13 at 7.29.35 PM

Add #include <canlib.h> 
Add the include directive: “#include “canlib.h” to your source code.

Screen Shot 2018-11-13 at 7.31.17 PM

Selecting platform x86 and x64

I can now select x86 (win32) and x64 (win64) as active platforms. In both cases, the application starts correctly and CANlib is available and working.

If everything worked well, you should also be able to create a C++ CANlib application in Visual Studio 2017.

We have now enabled CANlib in Visual Studio 2017, created a C++ project and tested it for both x86 and x64. 

ΟΕΔ  (Q.E.D.  Quod erat demonstrandum)


Distribution of code

Please remember, when using your application on a “client machine”, install “Kvaser Drivers” and “Microsoft Visual C++ Redistributable for Visual Studio 2017”.

Thanks for reading this! If you have any questions or comment regarding this paper, please send a mail to [email protected].

lgf-20201114-cr

Lars-Göran Fredriksson

Lars-Göran Fredriksson is a Field Application Engineer for Kvaser AB. His background is in geographic information system (GIS) and Remote Sensing and his current focus is on connecting the deep knowledge of Kvaser's developers with the practical questions of our end users. If you doubt his passion for CAN, just know that his first week in the office he created an interactive CAN Trivia game that sent the office scouring the halls for the correct answers. He is a passionate fisherman who would like to develop new environmentally friendly fishing methods. Biggest catch and release fish is for the moment a Bluefin Tuna at appr 325kg / 715lbs.