# How To Organize This C Code and Write Header File

**URL:** <https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276>\
**Category:** Factual Questions\
**Created:** [January 10, 2006, 4:43am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276 "2006-01-10T04:43:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [January 10, 2006, 4:43am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/1 "2006-01-10T04:43:02Z")

</div>

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:

```auto

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.c)  
[http://larsen.is-a-geek.com/serial\_nt.h](http://larsen.is-a-geek.com/serial_nt.h)  
[http://larsen.is-a-geek.com/serial\_nt\_main.c](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

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [January 10, 2006, 5:56am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/2 "2006-01-10T05:56:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Manduck](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/manduck/32/256_2.png) [@Manduck](https://boards.straightdope.com/u/Manduck)\
**Post date:** [January 10, 2006, 6:52am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/3 "2006-01-10T06:52:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Small\_Clanger](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Small\_Clanger](https://boards.straightdope.com/u/Small_Clanger)\
**Post date:** [January 10, 2006, 9:30am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/4 "2006-01-10T09:30:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SolGrundy](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SolGrundy](https://boards.straightdope.com/u/SolGrundy)\
**Post date:** [January 10, 2006, 9:45am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/5 "2006-01-10T09:45:14Z")

</div>

> [@Small Clanger](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [January 10, 2006, 11:22am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/6 "2006-01-10T11:22:54Z")

</div>

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:

```auto

#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.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [January 10, 2006, 11:37am UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/7 "2006-01-10T11:37:02Z")

</div>

> [@Derleth](#):
>
> (if HANDLE expanded out to the empty string, that is)

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.

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [January 10, 2006, 3:29pm UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/8 "2006-01-10T15:29:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [January 10, 2006, 5:12pm UTC](https://boards.straightdope.com/t/how-to-organize-this-c-code-and-write-header-file/339276/9 "2006-01-10T17:12:13Z")

</div>

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.
