2

I have a build bash script in the folder of my code which spits out the .o files but, sometimes, I find duplicates in my Home directory.

What's causing this and how do I prevent this?

Here is my build script:

#!/usr/bin/env bash
rm explorer-app
rm *.o
g++ -std=c++11 -fPIC -c *.cpp
g++ -std=c++11 *.o -fPIC -o explorer-app -L/usr/lib/SFML -L/usr/lib/Qt -Wl,-rpath,/usr/lib/SFML -Wl,-rpath,/usr/lib/Qt -lsfml-graphics -lsfml-window -lsfml-system -lQt5Core -lQt5Gui -lQt5Widgets
Andrew Bainbridge
  • 4,651
  • 3
  • 35
  • 50
Dan
  • 1,812
  • 3
  • 13
  • 27
  • Not sure why this is being downvoted. It's an honest problem I've never encountered before and it doesn't make sense – Dan Mar 30 '18 at 18:31
  • Could you update with the bash script? Or at least the part where you generate the .o? – Daniel Rodríguez Apr 03 '18 at 06:49
  • 2
    Please elaborate more on the problem, the info you have posted, won't allow anyone to be able to help much – Tarun Lalwani Apr 03 '18 at 09:12
  • The information that you have provided is not sufficient to answer. But what you are observing may be possible due different value of pwd when compiling the code. – Ritesh Apr 09 '18 at 14:53
  • @Ritesh added script – Dan Apr 09 '18 at 14:57
  • The script seems fine. But on the safe side, add a command as the first file to change pwd, something like cd srdir, srcdir represents the location of srcfile. That will eliminate the chances of o files being created anywhere else. But the best solution will be to use makefile. Please read the link https://stackoverflow.com/questions/1814270/gcc-g-option-to-place-all-object-files-into-separate-directory?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa , especially the answer from anon. – Ritesh Apr 09 '18 at 15:33
  • @Maverick: Can you try executing your script from different locations and see if you are able to reproduce the problem? – Ritesh Apr 09 '18 at 15:42
  • Where are you executing the make file? – 138 Apr 09 '18 at 17:20
  • @138 same dir as the script – Dan Apr 09 '18 at 17:57
  • running rm *.o at the end might be a quick n dirty fix, not sure if this is what you want though. Have yout though of using a makefile not a bash script? – 138 Apr 09 '18 at 18:50
  • The solution I'm looking for should pertain to using bash scripts. On that note I also do not like adding more dependencies than necessary. Makefiles are fossils and have convoluted syntax. Bash is simple and effective for most projects. – Dan Apr 09 '18 at 19:08
  • Since you are inclined to using the bash script, You can add few thing to your script, the first command will be to change the present working directory to what you really want it to be. Second when you generate the .o files, do them inside particular folder, and not along the src files. These 2 things will prevent any leakage of .o files. But still I am out of idea that how your .o files leaked. – Ritesh Apr 09 '18 at 19:38
  • I've added the 'cd .' command at the top of the script. We'll see if that makes a difference. It is strange. – Dan Apr 09 '18 at 21:11
  • cd . will not help. cd . will always set pwd to where you are. You need to add cd . – Ritesh Apr 10 '18 at 02:24
  • right I always run the bash script inside the same dir as the script. Just for completeness sake I'll make it the absolute path. Still does not address the problem completely. – Dan Apr 10 '18 at 15:59
  • Adding an absolute path did something different. Instead of spilling .o files in the root dir, some extraneous .o files are littered in the parent directory where I run the script from! @Ritesh – Dan Apr 16 '18 at 15:39
  • Can you specify what you meant by root dir and parent dir. It is not possible for .o files to get created out of dir if you have added cd at the top of your script. Is it possible to paste the updated script? – Ritesh Apr 16 '18 at 16:09
  • Root dir: `~/` aka Home – Dan Apr 16 '18 at 16:11
  • Parent dir. The literal directory above the one I'm building in. – Dan Apr 16 '18 at 16:12
  • Script did not change except I added a `cd ~/Code/project/` path. The script works and builds. Sometimes `.o` files appear in ~/Code/`. – Dan Apr 16 '18 at 16:12

0 Answers0