Compiling multiple .cpp and .h files using g++. Am I doing it right?

余生长醉 提交于 2019-12-08 03:42:00

问题


Lets say I have these files: main.cpp, ClassA.cpp, ClassA.h, ClassB.cpp, ClassB.h

  • The main has #include "ClassA.h" and #include "ClassB.h" and each .cpp file includes its respective .h file. Is this correct?

Now I am compiling using g++ *.cpp after which I get an executable a.exe (Windows)

  • And my question is that is this the right way? And lets say if I only make changes in one file (cpp or h)will this command also recompile the unchanged files(because I see no new files in the folder except a.exe)? Please explain. Also how do I prevent that?

P.S I am not familiar with make and do not want to use that either so please do not refer to that in the answers and I read Using G++ to compile multiple .cpp and .h files but I need more explanation regarding my questions.


回答1:


If you want to do it manually, you can compile all your .cpp files into object files

g++ -c *.cpp

and link all the object files

g++ *.o -o a.out

If ClassA.cpp is changed, you can just recompile ClassA.cpp

g++ -c ClassA.cpp

and link them all again

g++ *.o -o a.out

At least, you do not need to recompile the unchanged .cpp files.




回答2:


And my question is that is this the right way?

It will be better to create a makefile and use make to build your target.

And lets say if I only make changes in one file (cpp or h)will this command also recompile the unchanged files(because I see no new files in the folder except a.exe)?

Yes.

Please explain. Also how do I prevent that?

Using make. make will help keep track of dependencies and compile files that need to be compiled, or in general build targets that are out of date.




回答3:


For the #include, you are correct. Make sure you have include guards so that their contents are included only once.

#ifndef CLASS_A_H
#define CLASS_A_H

class classA
{

};

#endif

you can specify -o to g++ to name your output file something else than 'a.exe'

Usually with gcc/g++ and other command line compilers, the strategy is to first compile the source files, then link them with another command. This helps prevent the files not modified from being recompiled every time. This uses the -c switch of gcc

Try this

g++ -c ClassA.cpp -o ClassA.o
g++ -c ClassB.cpp -o ClassB.o
g++ -c main.cpp -o main.o

g++ ClassA.o ClassB.o main.o -o myProgram.exe



回答4:


You should use cmake for that purpose

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
project(cmake_demo)
    File(GLOB SRC_FILES src/*.cpp)
message(src: ${SRC_FILES})
add_executable(cmake_demo ${SRC_FILES})
  1. create CMakeLists.txt and past above code in that file
  2. create a folder called src put your all srource files in that folder
  3. then open terminal and move to this CMakeLists.txt
  4. create folder build
  5. go into that folder from terminal
  6. type (cmake .. ) then enter
  7. then type (make) then enter

it should be compiled and work the way you want :)

you folder structure should look like this

CMakeLists.txt
src
build

hope would help :)



来源:https://stackoverflow.com/questions/26814485/compiling-multiple-cpp-and-h-files-using-g-am-i-doing-it-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!