Using libmodbus with wxWidgets Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Using libmodbus with wxWidgets

Post by Ksawery » Thu Jul 25, 2019 12:45 pm

Hello,

I'm starting to learn wxWidgets through online tutorials, and so far I have successfully created a very basic GUI in Visual Studio 2019. I was planning on using wxWidgets to create a GUI for a Modbus RTU RS-232 Master application, which will communicate over the serial line with a Modbus slave microcontroller. I have successfully tested the Modbus application in a standalone C++ terminal program (the Modbus library is written in C, but also handles events where it is called from C++), and would now like to include the library in my wxWidgets GUI. However, as soon as I include the Modbus header file in any of the wxWidgets class headers, I get a ton of errors related to the wxWidgets files.

Here is a sample of my code:

cApp.h:

Code: Select all

#pragma once

#include "modbus.h"
#include "wx/wx.h"
#include "cMain.h"

class cApp : public wxApp
{
public:
	cApp();
	~cApp();

private:
	cMain* m_frame1 = nullptr;

public:
	virtual bool OnInit();
};
cApp.cpp:

Code: Select all

#include "cApp.h"

wxIMPLEMENT_APP(cApp);

cApp::cApp()
{
}

cApp::~cApp()
{
}

bool cApp::OnInit()
{
	m_frame1 = new cMain();
	m_frame1->Show();
	return true;
}
And here is a sample of the errors I get (there are 49 errors total, all of them in wxcrt.h and filefn.h:

Code: Select all

Severity	Code	Description	File	Line
Error	C4996	 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	C:\sdks\wx312\include\wx\wxcrt.h	209

Severity	Code	Description	File	Line
Error	C4996	 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	C:\sdks\wx312\include\wx\wxcrt.h	285

Severity	Code	Description	File	Line
Error	C4996	 'strncat': This function or variable may be unsafe. Consider using strncat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	C:\sdks\wx312\include\wx\wxcrt.h	308

Severity	Code	Description	File	Line
Error	C4996	 '_wopen': This function or variable may be unsafe. Consider using _wsopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	C:\sdks\wx312\include\wx\filefn.h	429
Is there a way to safely include the external C library, without breaking wxWidgets?

Any help would be greatly appreciated.

Regards,
Ksawery

User avatar
doublemax
Moderator
Moderator
Posts: 13989
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using libmodbus with wxWidgets

Post by doublemax » Thu Jul 25, 2019 1:00 pm

Usually these things only create warnings, not errors. Try defining _CRT_SECURE_NO_WARNINGS as stated in the message.
Also, try including any 3rd party header after any wx header.
Use the source, Luke!

Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Re: Using libmodbus with wxWidgets

Post by Ksawery » Thu Jul 25, 2019 3:34 pm

Thank you. Unfortunately changing the order of includes made things worse. I managed to get the application to run by suppressing the error/warnings using:

Code: Select all

#pragma warning(disable:4996)
However I'm unable to call the library functions:

Code: Select all

/* Initialize MODBUS */
modbus_t* ctx;
ctx = modbus_new_rtu("\\\\.\\COM4", 19200, 'E', 8, 1);
modbus_set_slave(ctx, 1);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS232);
and get the following errors:

Code: Select all

Severity	Code	Description	File	Line
Error	LNK1120	3 unresolved externals	C:\Users\kwret\source\repos\PomiarWiazki_wxWidgets\Debug\PomiarWiazki_wxWidgets.exe	1
Error	LNK2019	unresolved external symbol __imp__modbus_new_rtu referenced in function "public: __thiscall cMain::cMain(void)" ([email protected]@[email protected])	C:\Users\kwret\source\repos\PomiarWiazki_wxWidgets\PomiarWiazki_wxWidgets\cMain.obj	1
Error	LNK2019	unresolved external symbol __imp__modbus_rtu_set_serial_mode referenced in function "public: __thiscall cMain::cMain(void)" ([email protected]@[email protected])	C:\Users\kwret\source\repos\PomiarWiazki_wxWidgets\PomiarWiazki_wxWidgets\cMain.obj	1
Error	LNK2019	unresolved external symbol __imp__modbus_set_slave referenced in function "public: __thiscall cMain::cMain(void)" ([email protected]@[email protected])	C:\Users\kwret\source\repos\PomiarWiazki_wxWidgets\PomiarWiazki_wxWidgets\cMain.obj	1
I'm pretty sure that my library is included correctly in the project, so I'm really not sure what's going on. Will this library not be compatible with wxWidgets?

Regards,
Ksawery

User avatar
doublemax
Moderator
Moderator
Posts: 13989
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using libmodbus with wxWidgets

Post by doublemax » Thu Jul 25, 2019 4:35 pm

Undefined references just mean that these functions are not linked into the program. You may have to browse the library's source code (if it's accessible) and check if there are any preprocessor symbols that could make an external function unavailable.

Did you build the library yourself or did you get a binary? If you build it yourself, how?
Use the source, Luke!

Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Re: Using libmodbus with wxWidgets

Post by Ksawery » Fri Jul 26, 2019 9:09 am

Thanks for your reply. I built the library myself, using the instructions provided in the readme, at the following repository:

https://github.com/stephane/libmodbus

I built the library in Visual Studio 2019, Windows x64. I then linked the source/header and .dll files into the wxWidgets project, in the same way as I did in my test project (a simple C++ terminal program, which works OK).

If you would be willing to take a look, my libmodbus/wxWidgets project is at the following repository:

https://[email protected]/ ... idgets.git

My libmodbus test project (simple terminal application) is at the following repository:

https://[email protected]/ ... s_test.git

Both repositories should have all the necessary dependencies - apart from the wxWidgets installation (the WXWIN environemnt variable needs to be setup to point to the wxWidgets installation).

I will try to take a look at the pre-processor symbols - as per your recommendation - but I'm not sure what I should be looking for exactly.

Many thanks for your help.

Regards,
Ksawery

Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Re: Using libmodbus with wxWidgets

Post by Ksawery » Fri Jul 26, 2019 9:29 am

Could it be that the 64-bit compiled library may be in conflict with the 32-bit wxWidgets library? Now i'm not sure which version of wxWidgets I built, perhaps that's the issue here.

User avatar
doublemax
Moderator
Moderator
Posts: 13989
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using libmodbus with wxWidgets

Post by doublemax » Fri Jul 26, 2019 9:31 am

Ksawery wrote:
Fri Jul 26, 2019 9:29 am
Could it be that the 64-bit compiled library may be in conflict with the 32-bit wxWidgets library? Now i'm not sure which version of wxWidgets I built, perhaps that's the issue here.
Yes. 32 and 64 bit libraries are incompatible in general.
Use the source, Luke!

Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Re: Using libmodbus with wxWidgets

Post by Ksawery » Fri Jul 26, 2019 9:34 am

Thanks, I'll recompile wxWidgets to ensure its 64-bits and test the application again.

Regards,
Ksawery

Ksawery
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Jul 25, 2019 12:31 pm

Re: Using libmodbus with wxWidgets

Post by Ksawery » Fri Jul 26, 2019 10:03 am

Ok, I re-compiled libmodbus in 32-bits, instead of re-compiling wxWidgets in 64-bits, and...

Image

So it was 32/64 bit compatibility after all. I still needed to ensure the order of headers was correct and suppressed the _CRT_SECURE_NO_WARNINGS as before. Hopefully the application will keep working as I continue to build the GUI.

Best regards,
Ksawery

Post Reply