Home > Software design >  How compile a c complex folder with a simple command?
How compile a c complex folder with a simple command?

Time:11-23

I have a many c files in a folder:

> project
  - a.cpp
  - a.h
  - b.cpp
  - b.h
  - main.cpp
  - .editorconfig
  - .gitignore

And use this command to compile my code:

g   *.c* -o main

However I need organize my code in

> project
  > src
    > classes
      - a.h
      - b.h
    > methods
      - a.cpp
      - b.cpp
    - main.cpp

  - .editorconfig
  - .gitignore

Some questions:

1 - What command can I use to compile my project? 2 - Is it a good folder structure?

CodePudding user response:

I would try to compile it with the following command:

g   src/main.cpp src/methods/*.cpp -I src/classes -o myprogram

As long as your compilation times are reasonable, there is not much need to use a build system like Make yet.

I don't know if it's a good directory structure, but that's a pretty subjective question. It's more common to see directories named "include" or "header" than "classes", since it's possible to have header files that do not define classes. I usually like to put all of the source files in the same directory unless there are a lot of them, or some are written by different teams.

CodePudding user response:

As Louis Go just said, use some flavor of the make command. You then write a Makefile (that should be the file's name ...) which specifies what to do. There are plenty of online tutorials on the subject that will show you exactly how to do it.

CodePudding user response:

You can create a Makefile, then organize your header files in an "include" folder, and your source files in a "src" folder:

#change the project to your desired name
EXECUTABLE := project

# the source files to be built
SOURCES := *.cpp


INCLUDES := -I ../include
EXT := out
CC := g  

all:
    $(CC) $(INCLUDES) $(SOURCES) -o $(EXECUTABLE).$(EXT)

realclean:
    find . -type f -name "*.o" -exec rm '{}' \;
    find . -type f -name "*.exe" -exec rm '{}' \;
    find . -type f -name "*.out" -exec rm '{}' \;
    find . -type f -name "*.gch" -exec rm '{}' \;

The makefile will go in your "src" folder, and you would run command "make" to compile and run

  •  Tags:  
  • c
  • Related