Multiple .c files

This is a discussion on Multiple .c files within the Windows Programming forums, part of the Platform Specific Boards category; Hi! I'm working on some project and I want to write multiple .c files (each .c file for a dialog). ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Multiple .c files

    Hi!

    I'm working on some project and I want to write multiple .c files (each .c file for a dialog). Now I have main.c and about.c files and if I want to compile this I get these errors:

    About.obj : error LNK2005: _szAppName already defined in main.obj
    Release/Address Book.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.

    What is wrong and how to use multiple files?

    I'm using Visual C++ 5.0 on Win98.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Looks like you have 2 instances of a global variable......this is fine when compiling 1 source file....but when you try link tham all...it leads to conflicts...

    Look through your code for this error......and try limit the amount of globals that you use.......this will help minimise this problem

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    that is because you must separate the code for each module into pair of .c and .h file. Like this

    Code:
    ////////////////////////////////////////
    ///MyDlg.h
    //declaration file
    
    struct MyDlg
    {
    
    
                    HWND m_hWnd;
                    char title[255];
    };
    
    MyDlg gMyDlg;
    
    void Create( void );
    
    
    ////end of h file
    
    
    
    
    
    ///////////////////////////////////////
    //MyDlg.c
    //definition file
    #include"MyDlg.h"
    
    void Create( void )
    {
                
             .....use gMyDlg....here
    
    }
    
    ///end of c file
    
    
    /////////////////////////////////////
    //main
    #include"MyDlg.h"
    
    void main(void)
    {
    
    }
    
    
    remember that you must not include .c files only .h files
    zMan

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Thanks Fordy. The global variables were the problem.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 12:56 AM
  2. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  3. Difference between .h and .c files
    By Metalix in forum C Programming
    Replies: 9
    Last Post: 01-19-2005, 07:38 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21