1

I have a C project source (Linux kernel in this case), and Makefile.

I find this painful because I do modifications and I switch betwen branches and then I forget which commit these object files relate to.

Can I do version control automatically over the building process?

For example (just I think might be a good workflow, maybe it's silly):

  1. Force me make sure the git working spcace is clean before make

  2. Keep version of build automatically. Relate one build to both an object file and a source file.

  3. I can checkout any build

  4. I can know which .o files are up-to-date (compared with source code working space). Like git status

But most of all, the source code git tree can't be interfered.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Donald Wu
  • 107
  • 1
  • 9
  • These can be done in your Makefile. `clean` is a very common target. You could add a target to run git commands to track all the files after the main target is finished. – ElpieKay Dec 21 '17 at 05:33
  • I might be wrong but I think that when git modifies one of your source files, its modification date changes therefore if the Makefile is correctly written `make` will build it again. – Tim Dec 21 '17 at 11:43
  • Have you get the answer which helps you solve the problem? If yes, you can mark it as answer. And it will also benefit others who have similar questions. – Marina Liu Dec 26 '17 at 05:14

3 Answers3

2

A common solution is to embed information in the artefact to identify it.

For example, if you have a number of -D parameter=value settings controlling the compiler, put them in a string.

const volatile static char build[] = "parameter PARAMETER has value " PARAMETER;

Your can then have code which returns this string to the caller, or you can just inspect the binary with strings or less to see the information you embedded in it.

For more information, see Does gcc have any options to add version info in ELF binary file?

tripleee
  • 175,061
  • 34
  • 275
  • 318
2

The best practice about object files is to git-ignore them:

echo *.o >> .gitignore

The best practice about build process with branches is to have separate long-running worktrees for different branches:

git clone -b branch1 master-repo project-b1
git clone -b branch2 master-repo project-b2
cd project-b1
make
cd ../project-b2
make

And don't switch branches in these worktrees.

phd
  • 82,685
  • 13
  • 120
  • 165
1

Since .o file is an object file produced by C compiler (the output file from C project), it’s unnecessary to version control the .o file in git repo.

You just need to manage the source code in your git repo (such as C project and Makefile).

For the output files, you should ignore them in .gitignore:

touch .gitignore
echo **/*.o >> .gitignore
git add .
git rm **/*.o --cached 
git add .
git commit -m 'ignore .o files'
git push
Marina Liu
  • 36,876
  • 5
  • 61
  • 74