Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Recommended Posts

I've never actually had to deal with creating these before. I need a makefile that gives me separate object files for each source code file, so I can do a partial rebuild. Right now my Linux build takes several minutes to compile for any little change I made. The information on these is not very clear or readily available. This is my GCC command line.

How do I turn this into a working makefile?

"command": "/usr/bin/g++",
            "args": [
                "./Libraries/Plugin SDK/GMFSDK.cpp",
                "./Libraries/Plugin SDK/MemReader.cpp",
                "./Libraries/Plugin SDK/MemWriter.cpp",
                "./Libraries/Plugin SDK/TextureInfo.cpp",
                "./Libraries/Plugin SDK/Utilities.cpp",
                "./Libraries/Plugin SDK/half/half.cpp",
                "-lm", "-lX11", "-lpthread",
                "-no-pie", "-g"


Link to post
Share on other sites

I recall looking into this a while back. A makefile is more of a script file and you can execute other bash commands before the g++ call.

Every cpp file thrown at the compiler gets outputted as a .o file. Then you collect all the .o files and build them into the executable.

There is most likely a bash command to help determine what .cpp files are new/changed and only rebuild the needed .o files.

I haven't done this, and using a makefile generator just makes it all confusing. But I hope this was a bit insightful. If nobody else has better solutions, I can ask around.

Link to post
Share on other sites

I have never compiled C++ code with any other compiler (I've never compiled C++ code on Linux either), I've never put a shirt on back to front, I've also never eaten cereal without milk (I'll stop with the never ever's).

I took a look at this for you and I gotta say, it made me feel sick, it shook me to my very core 🤮

All the years of technological advances in GUI's and fancy displays and we are back to the terminal window.

Anyway, from some reading and watching it didn't seem too bad to pick up. I will warn you though this is my first time but, this should hopefully give you a starting point and if this doesn't help then I'm sorry and I will be buying one of those Men in Black mind erasing pens and we will all cuddle in a circle waiting for that big flash.

From what I found, the general layout of these makefiles tend to follow a similar pattern, where they are made up of a set of rules:

targets: prerequisites
  • Targets are the files you want to output (similar to building an executable with Visual Studio, your target application type would be a .exe).
  • Prerequisites are the files you would like to track, consider them dependencies that you need to use in order for any commands to work.
  • Commands are the functions run to produce the output.

Lets say you have like 5 files, a main.cpp and two other .cpp and .h files (lets call them functions.cpp and functions.h and utilities.cpp and utilities.h). Those files when compiled make ".o" files (I'm pretty sure you are probably aware of this, me on the other hand ... I had no clue). If we extend this to the above pattern to achieve an automated build by calling the "make" command from your projects directory:

# output is our target that is built from our C/C++ source files, it depends on all other source files that are included within it
output: main.o functions.o utilities.o
	g++ main.o functions.o utilities.o -o output

# main.o is output only when main.cpp is changed (main.o depends on main.cpp), we run g++ -c main.cpp to produce main.o
main.o: main.cpp
	g++ -c main.cpp

# functions.o is output only when functions.cpp or functions.h is changed (functions.o depends on functions.cpp and functions.h), we run g++ -c functions.cpp to produce functions.o
functions.o: functions.cpp functions.h
	g++ -c functions.cpp

# utilities.o is output only when utilities.cpp or utilities.h is changed (utilities.o depends on utilities.cpp and utilities.h), we run g++ -c utilities.cpp to produce utilities.o
utilities.o: utilties.cpp utilities.h
	g++ -c utilities.cpp

# clean is a command that can be executed to remove all ".o" files (consider this to be very similar to running "Clean solution" in Visual Studio, you know ... when it removes all of your intermediary files and folders)
	rm *.o output

There are some subtle syntax shenanigans at play here too, like you have to have 4 spaces before each command.

I found this for you which goes further than my explanation and there are some nice code examples too. Depending on how much time you really want to put into this I'm sure you could do some very complex automated build tasks (and it would certainly set you up in the long run). What I mentioned above though should get you by, although all those source files you have would be really tedious to have to type, you can do something called "pattern rules" which can form expressions to define more complex rules than the ones I wrote above.

Good luck!

Link to post
Share on other sites

Example how to do using cmake which generates different kind of makefiles.

Create a CmakeList.txt file in your project with content below and modify/add your sources.

Will generate the makefiles:

cmake .

then build:







    set(CMAKE_CXX_FLAGS "-std=c++11")


    add_executable (nerva source/main.c source/cmd_handlers.c source/nerva_utils.c source/sqlite3.c)

    target_link_libraries(nerva m dl pthread)


    add_executable (nerva source/main.c source/main.c source/cmd_handlers.c source/nerva_utils.c source/sqlite3.c)




cmake is great if you want a build system that will work on all platforms , can generate also visual studio projects on windows.

But it has its quirks i seen in some big projects using it (and that require some time reading docs or workarounds).


So hand written makefile is  good option also if you want just for linux (or mac) and full control of what is going on.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...