pdeva
pdeva

Reputation: 45451

Compiling multiple source directories with g++

My c++ project has the following structure

src
|
|source1.cpp
|source2.cpp
|
|<srcfolder1>
|__ source11.cpp
|__ source12.cpp
|
|<srcfolder2>
|__ source21.cpp
|__ source22.cpp

As can be seen there are multiple folders with multiple source files. What command do i give g++ to compile all the source file into a single .o file?

Upvotes: 5

Views: 8630

Answers (4)

rusini
rusini

Reputation: 1

Perhaps, the most direct answer is something like:

g++ $(find src -name '*.cpp')

That said, having so many translation units, having a Makefile helps to dramatically reduce build times as you edit your source files and build to test.

Upvotes: 0

xalpha
xalpha

Reputation: 446

If you are used to working with Visual Studio and you have to compile your files under linux, there is no need to start doing everything on the command line ;)

Like GradGuy pointed out, QtCreator is a great IDE and I also use it for my non-windows builds. However, if you don't do any GUI stuff, you might want to use it with together with cmake as Qt is mostly about GUIs.

cmake is a generator for the solution files (like VS' .sln files) that is independent of what IDE or you operating system you are running. It is a bottom-up approach where you need very little to just get going.

Here are some steps to get you started

  • install qtcreator and cmake
  • write a file telling cmake where your source files are and where to find them.
  • create a file CMakeLists.txt in the folder above src and write inside the following:

    project( myProject )
    
    # set your include directories (if you have any)
    include_directories( include )
    
    # tell cmake what what are your source files
    set( MY_CPP_FILES 
        src/source1.cpp
        src/source2.cpp
        src/srcfolder1/source11.cpp
        src/srcfolder1/source12.cpp
        src/srcfolder2/source21.cpp
        src/srcfolder2/source22.cpp )
    
    # if you are creating an executable then do so like this
    # note that ${MY_CPP_FILES} will replace it with its content
    add_executable( myExec ${MY_CPP_FILES} )
    
    # if you instead want a library, do it like so
    add_library( myStaticLib STATIC ${MY_CPP_FILES} ) # create a static library
    add_library( mySharedLib SHARED ${MY_CPP_FILES} ) # create a shared library
    
  • now start QtCreator and choose File -> Open File or Project and select CMakeLists.txt on your drive

  • now you will be asked to choose a path where the results will be stored (as well as all intermediate files)
  • hit Run CMake
  • and finally choose Build -> Build All

Good Luck!

Upvotes: 1

mmirzadeh
mmirzadeh

Reputation: 7079

You could also use build systems that would generate the actual makefile for you. This becomes important as you try to expand your project and maintaining a good makefile might become a daunting task. I suggest that you spend some time to learn one. Examples are Scons, GNU autotools, qmake, and Cmake, among others. I highlly reccomond the use of qmake or Cmake as they are both cross-platform and easy to learn.

Another option that you have is to simply use an IDE! The best c++ IDE on Linux that I know of, and is cross-platform, is Qt Creator. Under the hood it supports both qmake and Cmake projects.

Upvotes: 2

Rolle
Rolle

Reputation: 2980

If you have many source files you should really consider writing a Makefile: http://mrbook.org/tutorials/make/

You can for example use a syntax similar to this to compile multiple files (in the Makefile):

%.o : $(SRC_FOLDER)/%.cpp
  $(CXX) -c -o $@ $<

Upvotes: 6

Related Questions