Thread: #ifndef is not working as expected.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by boqsc
    It's all about trying to make a simple modular project.

    I'm intentionally including .c files instead of making a seperate .h file to make it a single portable source file.

    I'm about to make a single source file for each module and I want that each module would be runnable by using Tiny C Compiler.
    Unfortunately for you, C doesn't work that way. There is no notion of modules as a language feature. What you will end up with is a collection of self-contained programs, each in a single source file.

    Quote Originally Posted by boqsc
    Why I want that instead of compiling into .exe file? Why I want a runnable .c file? So that we could edit C language programs and share around instead of executable binaries.
    You can already do that by editing and sharing C source files and their associated headers.

    Quote Originally Posted by boqsc
    I need main method for each .c file and I'm importing each .c into other .c files and vice versa.
    You cannot get around the fact that there can be only one main function in a C program, so what you are trying to do is doomed to fail.

    Quote Originally Posted by boqsc
    So let's go back to the (instead):
    That's exactly what your compiler is telling you with the error message about "redefinition of 'main'": there can be only one main function in a C program. There may be a hacky workaround: define the main function in each source file, then #define main some_random_name before including the source file and then #undef main after including it. But you're still going to run into other multiple definition errors when your inclusion graph becomes more complex. It may be possible to avoid redefinition errors using the "header-only" approach seen in C++ by using the inline keyword liberally, but that assumes that your target compiler supports it sufficiently.

    The way I see it, if this approach is particularly important to you, you would be better served by switching to a programming language that would enable you to do what you want to do.
    Last edited by laserlight; 07-12-2021 at 09:16 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not working as expected
    By JayCee++ in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2012, 05:23 AM
  2. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  3. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 01:49 PM
  4. srand() no working as expected
    By guesst in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 12:24 PM

Tags for this Thread