How To Organize This C Code and Write Header File

I’m working on some straight C code that will be compiled in as part of a larger project. I hacked up what I thought to be decent enough code that compiled and worked fine on it’s own. When I inserted this into the larger application, though, the compiler (VC++) choked. I think I’m mixing C++ and C when all I really want to C to fit into the rest of the application. My C/C++ skills, I’m afraid, are rusty.

My code is simple enough. It is the start of serial port communications for the NT environment. Optimally, I would want a single C file containing my functions (one to initialize the serial port, one to read from it, one to write from it - I’ve already coded these functions - see links below). I would open the serial port once and then be able to call the write and read functions anywhere else in the app using the open handle.

Compiling my c, h and another file to call the functions into a console app works great. Inserting this into the larger application chokes with the following:


c:\src\serial_nt.h(1) : error C2061: syntax error : identifier 'SerialInit'
c:\src\serial_nt.h(1) : error C2059: syntax error : ';'
c:\src\serial_nt.h(1) : error C2059: syntax error : 'type'
c:\src\serial_nt.h(3) : error C2143: syntax error : missing ')' before '*'
c:\src\serial_nt.h(3) : error C2143: syntax error : missing '{' before '*'
c:\src\serial_nt.h(3) : error C2059: syntax error : ')'
c:\src\serial_nt.h(3) : error C2059: syntax error : ';'
c:\src\serial_nt.h(5) : error C2143: syntax error : missing ')' before '*'
c:\src\serial_nt.h(5) : error C2143: syntax error : missing '{' before '*'
c:\src\serial_nt.h(5) : error C2059: syntax error : ','
c:\src\serial_nt.h(5) : error C2059: syntax error : ')'
c:\src\serial_nt.h(7) : error C2143: syntax error : missing ')' before '*'
c:\src\serial_nt.h(7) : error C2143: syntax error : missing '{' before '*'
c:\src\serial_nt.h(7) : error C2059: syntax error : ')'
c:\src\serial_nt.h(7) : error C2059: syntax error : ';'
c:\src\serial_nt.h(9) : error C2143: syntax error : missing ')' before '*'
c:\src\serial_nt.h(9) : error C2143: syntax error : missing '{' before '*'
c:\src\serial_nt.h(9) : error C2059: syntax error : ','
c:\src\serial_nt.h(9) : error C2059: syntax error : ')'
c:\src\instr_io.c(576) : warning C4013: 'SerialInit' undefined; assuming extern returning int
c:\src\instr_io.c(578) : warning C4013: 'SerialPuts' undefined; assuming extern returning int

If anyone is still with me, my source files are here (the main is just for the console test app):
http://larsen.is-a-geek.com/serial_nt.c
http://larsen.is-a-geek.com/serial_nt.h
http://larsen.is-a-geek.com/serial_nt_main.c

I’m really curious why the compiler is dying on the header file? Am I mixing C/C++? Also, what is the best way to organize this code? Would i need my handle to the serial port connection to be global? I know that my reading will have to occur in a whole seperate thread, but I’ll cross that bridge when I come to it.

Thanks,

LarsenMTL

I think all the errors are fall-out from the undefined “SerialInit” error.

I don’t see where you’re including the header file in the .c file - am I missing something?

I’m wondering if you’re including serial_nt.h in a context where HANDLE is not defined. My c is even rustier than yours though, so I don’t know whether HANDLE is part of stdio or what. It seems reasonably likely that that is the problem.

Those files you supplied - I assume that is the small program that works, not the larger one that doesn’t, right?

Anyway, if HANDLE is part of stdio.h, then you should put #include <stdio.h> at the top of your serial_nt.h file. The standard .h files contain code that prevents multiple inclusion from being a problem, IIRC, in case you’re worried about that.

I’m with Manduck in suspecting there’s a problem with HANDLE. What does HANDLE expand to? Can you look in an intermidiary file to see what it is in your console version and compare it to the broken version?

HANDLE is usually defined by windows.h (at least it was the last time I did any windows programming) as an INT or LONG.

I think Manduck is right, and your serial_nt.h is getting included before you think it should. Based on the error log, the first implementation file it’s hitting is “instr_io.c.” Is that one of your files, or a system file? And is it including serial_nt.h?

I’d start just by adding this line to the top of serial_nt.h:

#include <windows.h>

And if that compiles okay (because HANDLE is now defined), then you can think of some more elegant way to fix it. (Either remove the errant include of serial_nt.h, or do a forward declaration of HANDLE).

I agree with SolGrundy, but keep in mind that I don’t know anything about Windows-specific C programming. (All my experience is POSIX, the Unix standard API.)

If HANDLE is defined like this:



#define HANDLE long


Then you will need to #include <windows.h> in any file whatsoever that uses the type, for obvious reasons. That would appear to be consistent with the error messages you are getting (if HANDLE expanded out to the empty string, that is). Inspect windows.h to see if I’m close to being right.

Or not an empty string, but an unknown token. And I think I’m wrong, in that a typedef would get the same effect. But that could be wrong, too.

Wow, Thanks all.

Manduck, Small Clanger, SolGrundy, Derleth you were all correct, my problem was in the Handle. I tried including windows.h in my serial_nt.h but while this fixed my problem it just broke the larger application. windows.h is too much of a behemoth and it breaks a lot of the custom typing throughout the application.

I’m working now on keeping the Handle local to my c file (it really doesn’t need to be passed around). Keep your fingers crossed.

Thanks,

LarsenMTL

Sorry to keep posting to my own thread, but I just wanted to report SUCCESS!!

Thanks again.

Mods, feel free to close yet another successfully answered GQ.