Thread: Includes making me insane >.<

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Includes making me insane >.<

    Alright - I got my biggest project not too long ago, and I've been working on it like mad lately, and just until an hour ago, I've have all my functions in one file.

    Now I'm trying to split it up and make it look good, all that, and I've run into tons of problems.

    Basically, my code structure is like this(I've checked the faq thread, tried to follow it, but the example is just way too small to fit my code)

    I'll just post the includes to explain what I'm talking about:


    Code:
    // main.c
    
    
    #include "data.h"
    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <all-the-other-system-header-files.h>
    
    #include "defines.h" // All the #defines and global variables, structures, etc.
    
    #include "resource.h"
    #include "necessary-device-headerfile.h"
    
    #include "source-code-files.c"
    
    // Prototypes for functions go here.
    None of the source code files have any includes. However, when I try to compile, I get errors that look like:

    Code:
    1>------ Build started: Project: RFIDApplication, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.c
    1>c:\programming\c++\rfidapplication\main.c(14) : warning C4189: 'ret' : local variable is initialized but not referenced
    1>c:\programming\c++\rfidapplication\main.c(190) : warning C4100: 'nCmdShow' : unreferenced formal parameter
    1>c:\programming\c++\rfidapplication\main.c(190) : warning C4100: 'lpCmdLine' : unreferenced formal parameter
    1>c:\programming\c++\rfidapplication\main.c(189) : warning C4100: 'hPrevInstance' : unreferenced formal parameter
    1>c:\programming\c++\rfidapplication\main.c(189) : warning C4100: 'hInstance' : unreferenced formal parameter
    1>rfidhandlers.c
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(29) : error C2146: syntax error : missing ')' before identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(29) : error C2061: syntax error : identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(29) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(29) : error C2059: syntax error : ','
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(29) : error C2059: syntax error : ')'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(154) : error C2146: syntax error : missing ')' before identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(154) : error C2061: syntax error : identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(154) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(154) : error C2059: syntax error : ','
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(154) : error C2059: syntax error : ')'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(200) : error C2146: syntax error : missing ')' before identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(200) : error C2061: syntax error : identifier 'rfidHandle'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(200) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(200) : error C2059: syntax error : ','
    1>c:\programming\c++\rfidapplication\rfidhandlers.c(200) : error C2059: syntax error : ')'
    1>fileprocs.c
    1>c:\programming\c++\rfidapplication\fileprocs.c(23) : error C2146: syntax error : missing ')' before identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\fileprocs.c(23) : error C2061: syntax error : identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\fileprocs.c(23) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\fileprocs.c(23) : error C2059: syntax error : ')'
    1>c:\programming\c++\rfidapplication\fileprocs.c(24) : error C2449: found '{' at file scope (missing function header?)
    1>c:\programming\c++\rfidapplication\fileprocs.c(113) : error C2059: syntax error : '}'
    1>cleanup.c
    1>c:\programming\c++\rfidapplication\cleanup.c(25) : error C2146: syntax error : missing ')' before identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\cleanup.c(25) : error C2061: syntax error : identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\cleanup.c(25) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\cleanup.c(25) : error C2059: syntax error : ','
    1>c:\programming\c++\rfidapplication\cleanup.c(25) : error C2059: syntax error : ')'
    1>dlghandlers.c
    1>c:\programming\c++\rfidapplication\dlghandlers.c(21) : error C2146: syntax error : missing ')' before identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\dlghandlers.c(21) : error C2061: syntax error : identifier 'hWnd'
    1>c:\programming\c++\rfidapplication\dlghandlers.c(21) : error C2059: syntax error : ';'
    1>c:\programming\c++\rfidapplication\dlghandlers.c(21) : error C2059: syntax error : ','
    1>c:\programming\c++\rfidapplication\dlghandlers.c(21) : error C2059: syntax error : ')'
    1>Generating Code...
    1>Build log was saved at "file://c:\programming\C++\RFIDApplication\Debug\BuildLog.htm"
    1>RFIDApplication - 31 error(s), 5 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    The rfidHandle is a type defined in the necessary device header file. hWnd has type HWND(duh) and is a handle to a window, of course, and for some reason, these two types seem to get errors for only those two types. There are at least 10 more types in use in the source files that are located in some of the system header files that don't get complained about.

    I'd really appreciate any swift answers - any suggestions if you don't know the answers, since I need to send the hardware off to the owner tomorrow morning which would mean that developing software for it wouldn't really be safe.

    Eagerly awaiting your aid.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Source files need includes. At least defines.h in all of them; any file that uses HWND is going to have to have windows.h. And so on.

    Remember: each file is compiled completely separately from everything else; any headers needed in that file have to be included.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by tabstop View Post
    Source files need includes. At least defines.h in all of them; any file that uses HWND is going to have to have windows.h. And so on.

    Remember: each file is compiled completely separately from everything else; any headers needed in that file have to be included.
    If I include windows.h in the source files, I get even more errors. Worst of all, visual studio's errors are really, really, REALLY sucky, and talk about missing this and that before or after this or that, instead of unknown identifiers if I'm missing a type declaration.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Wait, are you including a .c file in your main file? That's ... interesting. You shouldn't be doing that. (They'll each make a .obj file, which will get linked into an .exe.) Your .h files should have structs and function prototypes; function bodies should live in however many source code files you want them to; and then they all get linked together to live in perfect harmony at the end.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by tabstop View Post
    Wait, are you including a .c file in your main file? That's ... interesting. You shouldn't be doing that. (They'll each make a .obj file, which will get linked into an .exe.) Your .h files should have structs and function prototypes; function bodies should live in however many source code files you want them to; and then they all get linked together to live in perfect harmony at the end.
    I was at the time, yes, but now I've changed the layout and i'm still struggling.
    I'm actually down to 90 errors. Yes, they were more.

    Right now, the only issue seems to be multiple definition in the device header file.

    One question: If I have all the source files include another header file that includes the device file, resource.h and defines.h, would that be valid?

    I just had them include all of those files in each source file(except main) and it didn't work - gave me redefinition errors.

    EDIT:

    Basically, what I'm trying to do here is more complex than just setting up the function bodies and prototypes.. It's also a number of header files which most functions require to function and so on - AFAIK, you can put the header includes (the default ones, like windows.h and stuff, and all others of course) in one header file and make the files include that ONE header file to avoid repition, and it should work, yes? That's basically what I'm struggling with. Trying to putting the puzzle together.
    Last edited by IceDane; 04-13-2008 at 06:20 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you getting redefinition errors for global variables? Someone the other day was just asking about that (over in the C++ forum) -- extern may be your friend here.

    If you're getting redefinition errors for structures, then something's getting included twice.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by tabstop View Post
    Are you getting redefinition errors for global variables? Someone the other day was just asking about that (over in the C++ forum) -- extern may be your friend here.

    If you're getting redefinition errors for structures, then something's getting included twice.
    One quick question: Am I even supposed to include any .c files at all, in any header file? Cause I just remove those includes, and voila, something's way different..

    Now I get some unresolved externals and redefinition for the one global variable that I use. However, extern does not seem to solve it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're including .c files, something is very wrong.

    For globals: headers should say extern int bob; and one (and only one) .c file should contain an actual definition like int bob;.

  9. #9
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by tabstop View Post
    If you're including .c files, something is very wrong.

    For globals: headers should say extern int bob; and one (and only one) .c file should contain an actual definition like int bob;.
    Zomg. I want to have your babies.

    Thanks, I got it now. I had somehow confused myself into believing that I had to include the source code files, ignoring the fact that they get made into object files.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by IceDane View Post
    Zomg. I want to have your babies.
    Don't tempt me with things to quote in my signature!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by IceDane View Post
    If I include windows.h in the source files, I get even more errors. Worst of all, visual studio's errors are really, really, REALLY sucky, and talk about missing this and that before or after this or that, instead of unknown identifiers if I'm missing a type declaration.
    Actually, I would like to say they're pretty good. GCC's warnings, for example, are obscure and un-professional looking. You're merely throwing the compiler off-guard.
    The linker errors are horrible, I'll agree, though.

    Guidelines for headers are:
    - Include declarations and class definitions.
    - Include templates.
    - Do NOT include any sort of code (except for templates) and NO variables (extern declarations are fine).
    - Use include guards in every header.
    - NEVER include any source file, ONLY include other headers.

    Guidelines for source files are:
    - Do NOT put any declarations in the source files.
    - Put all your code in source files.
    - Include all the necessary headers from the source files.
    - NEVER include another source file.
    - AVOID putting templates into source files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Actually, I would like to say they're pretty good. GCC's warnings, for example, are obscure and un-professional looking. You're merely throwing the compiler off-guard.
    The linker errors are horrible, I'll agree, though.
    You're going to have to back up such an inflammatory statement.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't have GCC. If someone wants to post some errors from GCC, I can do equalient for Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I don't have GCC. If someone wants to post some errors from GCC, I can do equalient for Visual Studio.
    You don't have gcc, can't produce any errors from gcc, but you claim they suck?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I've seen plenty on the forum. Plenty, plenty.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. Making dll's in C???
    By frolicchap in forum C Programming
    Replies: 2
    Last Post: 02-03-2003, 05:15 AM
  3. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  4. Making A bootable windows cd...
    By Korn1699 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-30-2001, 11:53 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM