Author Topic: Problems building C++ code using a Serial library  (Read 7493 times)

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Problems building C++ code using a Serial library
« on: June 16, 2020, 04:28:44 pm »
Dear All,

First post here.  I'm quite new to CodeBlocks and working on a wxWidgets based GUI that uses serial coms to talk to an external device.

I got the program to work (using Windows 7) when using CreateFile to setup the serial port.  Now I'd like to use a library found here:
https://www.codeproject.com/Articles/992/Serial-library-for-C

I put the serial.cpp and serial.h files in the project folder but when I go to build, I get the following error.

undefined reference to 'CSerial::Open(......)

This is the function that is supposed to open a serial port.  Has anyone else had issues with this library in CodeBlocks?  It was initially used with MS VS circa 2003.  I have included serial.h at the top of the main program file.  Thank you.


Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Problems building C++ code using a Serial library
« Reply #2 on: June 16, 2020, 05:05:00 pm »
I put the serial.cpp and serial.h files in the project folder but when I go to build, I get the following error.

Did you add serial.cpp to the CB project? If not, try doing so.

If you do not wish to add serial.cpp to the CB project; then, you need to build the library and link to it.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Re: Problems building C++ code using a Serial library
« Reply #3 on: June 16, 2020, 05:22:57 pm »
I put the serial.cpp and serial.h files in the project folder but when I go to build, I get the following error.

Did you add serial.cpp to the CB project? If not, try doing so.

If you do not wish to add serial.cpp to the CB project; then, you need to build the library and link to it.

Tim S.

Hello Tim,
Yes, I added the serial.cpp and .h files to the project and they appear in the sources and headers tree. 

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Re: Problems building C++ code using a Serial library
« Reply #4 on: June 16, 2020, 05:30:46 pm »
I am running Code::Blocks version 20.03 on Windows 7. The compiler I use is GCC default (don't know which version number).

Hope this helps.

Build log:
Code
-------------- Build: Debug in AOR8600 (compiler: GNU GCC Compiler)---------------

g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -Wall -g -IC:\SDK\wx313CB\include -IC:\SDK\wx313CB\lib\gcc_lib\mswu -c C:\Users\X\Documents\MyCodeBlocks\AOR8600\AOR8600Main.cpp -o obj\Debug\AOR8600Main.o
g++.exe -LC:\SDK\wx313CB\lib\gcc_lib -o bin\Debug\AOR8600.exe obj\Debug\AOR8600App.o obj\Debug\AOR8600Main.o obj\Debug\portsEnum.o obj\Debug\Serial.o  obj\Debug\resource.res -mthreads -static  -lwxmsw31u -lwxpng -lwxjpeg -lwxtiff -lwxzlib -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -lshlwapi -lversion -loleacc -luxtheme -mwindows
C:\Users\X\Documents\MyCodeBlocks\AOR8600\AOR8600Main.cpp: In member function 'void AOR8600Frame::OnRecTimer(wxTimerEvent&)':
C:\Users\X\Documents\MyCodeBlocks\AOR8600\AOR8600Main.cpp:177:11: warning: unused variable 'n' [-Wunused-variable]
     DWORD n=0;
           ^
obj\Debug\AOR8600Main.o: In function `AOR8600Frame::OnconnectButtonClick(wxCommandEvent&)':
C:/Users/X/Documents/MyCodeBlocks/AOR8600/AOR8600Main.cpp:201: undefined reference to `CSerial::Open(wchar_t const*, unsigned long, unsigned long, bool)'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 10 second(s))
2 error(s), 1 warning(s) (0 minute(s), 10 second(s))
 

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Problems building C++ code using a Serial library
« Reply #7 on: June 16, 2020, 08:57:57 pm »
Quote
Yes, I cleared previous build data and rebuilt. 
There is no sign of the serial.cpp in your build log, so you have not added it to the project properly...

Check if there are ticks in Project->Properties->Build targets->Select your target on the left->Check if all source files are ticked in "Build target files"

Edit: Have you included Serial.h?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Problems building C++ code using a Serial library
« Reply #8 on: June 17, 2020, 01:50:09 am »
Edit: Have you included Serial.h?
Yes, if it wasn't included there would have been compiler not linker errors.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Re: Problems building C++ code using a Serial library
« Reply #9 on: June 17, 2020, 08:54:24 am »
Quote
Yes, I cleared previous build data and rebuilt. 
There is no sign of the serial.cpp in your build log, so you have not added it to the project properly...

Check if there are ticks in Project->Properties->Build targets->Select your target on the left->Check if all source files are ticked in "Build target files"

Edit: Have you included Serial.h?

I did go to add the files, but when I checked their properties by right clicking on the files in headers and sources and then selecting the Build tab, the check boxes for Compile file and Link file were not checked.  This was true for the other headers.
I have attached the project as a zipped file here, if someone would be so kind as to take a look.

The strange thing is that if they are not being compiled or linked, I do not get an error regarding the serial.setup(...) function.
I am trying to use these functions in the button to "connect" code in the AOR8600Main.cpp file.  All of the wxWidgets is working fine so far.

The aim is, when the program runs, the user will select one of the available COM ports, and when they click connect, the COM port should be opened followed by the function to set up it's parameters.  I'm getting stuck at the first hurdle when trying to use this library.

My commented out code for opening and setting up the port manually worked fine but its quite difficult to listen for and receive data, hence the use of this library that handles this very well.
Thank you

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Re: Problems building C++ code using a Serial library
« Reply #10 on: June 17, 2020, 09:40:45 am »
Quote
Yes, I cleared previous build data and rebuilt. 
There is no sign of the serial.cpp in your build log, so you have not added it to the project properly...

Check if there are ticks in Project->Properties->Build targets->Select your target on the left->Check if all source files are ticked in "Build target files"

Edit: Have you included Serial.h?

I double checked this and the serial.h and serial.cpp files are checked in the "Build Target Files" window. 
However when I check the files in the Headers tree in the Workspace, they are in grey not black.  When I right click on a file then select properties, the check boxes for Compile file and Link file are unchecked.  Could this be a problem? It doesn't seem to match the previous check when checking the Build Targets from Project->Properties.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Problems building C++ code using a Serial library
« Reply #11 on: June 17, 2020, 10:36:06 am »
Interestingly this is some bulls***** with the microsoft types...

what LPCTSTR is not clear for me and probably also the linker/compiler...
it is a (long) pointer to constant TCHAR string
and TCHAR is wchar_t if UNICODE is defined and char otherwise....

Somehow during compiling TCHAR seems to be wchar_t and during linking it seems to be char
If i modify your serial open function signature to
Code
virtual LONG Open(const char* lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);
it links and compiles fine for me.

Ultimately this is not a codeblocks problem....

[EDIT:] i did not tested it, and i do not know what type or encoding (probably utf-16) LPCTSTR has during runtime. So you still can get some crash compiling it that way...

[EDIT2:] Probably some windows guru can help you better or bring light in the dark API
« Last Edit: June 17, 2020, 10:38:53 am by BlueHazzard »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Problems building C++ code using a Serial library
« Reply #12 on: June 17, 2020, 10:41:35 am »
The right way to do this is probably add
Code
UNICODE
in Project->Build options->Select your project name on the left->Compiler settings->#defines

i was thinking
Quote
wxUSE_UNICODE
expands to this on windows, but i was wrong....

Offline PaulUK

  • Single posting newcomer
  • *
  • Posts: 7
Re: Problems building C++ code using a Serial library
« Reply #13 on: June 17, 2020, 10:52:20 am »
Interestingly this is some bulls***** with the microsoft types...

what LPCTSTR is not clear for me and probably also the linker/compiler...
it is a (long) pointer to constant TCHAR string
and TCHAR is wchar_t if UNICODE is defined and char otherwise....

Somehow during compiling TCHAR seems to be wchar_t and during linking it seems to be char
If i modify your serial open function signature to
Code
virtual LONG Open(const char* lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);
it links and compiles fine for me.

Ultimately this is not a codeblocks problem....

[EDIT:] i did not tested it, and i do not know what type or encoding (probably utf-16) LPCTSTR has during runtime. So you still can get some crash compiling it that way...

[EDIT2:] Probably some windows guru can help you better or bring light in the dark API

Ah I see.  I just made the change to both the serial.h and .cpp files and it compiles without error.  Well, your help has got me one step further and I will replace all occurrences of "LPCTSTR" with "const char*" in the library.   I didn't know it was a Microsoft related type. 

I'm hoping that I can use CodeBlocks as my main IDE instead of Microsoft and in the last few months I have been trying it out.  I'm no expert and still run into brick walls with issues such as this, but as you point out its a MS issue not CB.  Thank you for looking into it, I really appreciate it.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Problems building C++ code using a Serial library
« Reply #14 on: June 17, 2020, 11:27:37 am »
Quote
Ah I see.  I just made the change to both the serial.h and .cpp files and it compiles without error.  Well, your help has got me one step further and I will replace all occurrences of "LPCTSTR" with "const char*" in the library.   I didn't know it was a Microsoft related type.
See my second answer. It is probably better!!!