1

I have several object files coming from different directories (they are stored near the corresponding source that generated them). Is there a way that given this directory structure

Root

main.o

Root/Some_long_path

object_1.o
object_2.o

I could run a command like this

g++ -Wall main.o -ISome_long_path object_1.o object_2.o -o app

So that I don't have to put the full path in front of every object file. What would go instead of the -I command?

I am using gcc version 4.8.3 (from Cygwin installation).

Kvothe
  • 1,819
  • 2
  • 23
  • 37
  • There is `-L` option for searching libraries (`lib*.so` and `lib*.a` in unix terms), and seems there is no gcc option for object files (https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options and https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options). You can use make + makefiles to put right path automatically. – osgx Mar 12 '15 at 02:37
  • You shouldn't need to put a full path, use a relative path. – Barmar Mar 12 '15 at 02:38
  • @osgx Thanks, I'm going to have to spend some time today learning how to write/use makefiles. Some of my code is used in several applications, e.g. input validation modules, I feel this should be treated like a library - how would I create a library file? – Kvothe Mar 12 '15 at 10:19
  • @Barmar I meant is there a way to avoid writing out the full relative path each time. All the paths are relative (Root is the base folder of my application, not my system) – Kvothe Mar 12 '15 at 10:20
  • You could put the long path prefix in a variable and use `$dir/object_1.o`. – Barmar Mar 12 '15 at 12:00
  • @Barmar Thanks, I've just done that and it works great :) Going to start using a makefile now though, I just hadn't come across them before – Kvothe Mar 12 '15 at 13:48
  • In the makefile you can use `vpath` – M.M Mar 13 '15 at 02:41

1 Answers1

2

So that I don't have to put the full path in front of every object file. What would go instead of the -I command?

There is no gcc option like -I to add some directory to the object "search path", and there is no search path for objects. But there is search path for libraries (usually named lib*.a for static libraries and lib*.so for shared libraries in Unix world).

Directory-Options manual of gcc lists only -I option for include paths and -L option for library paths. There is no object path:

https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options

And Link-Options manual of gcc mention only -L option (near -l description):

https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options

What can you use:

  • shell variables or environment variables (in bash: OBJ_DIR=./path/to/dir then g++ ... $OBJ_DIR/obj1.o $OBJ_DIR/obj2.o)
  • Packing several objects from single directory to single static library libSome_Long_Component_Name.a (this type of library is just like the archive of several *.o object files and some extra info made with ar rcs); then you can use g++ ... -Ldir/ -lSome_Long_Component_Name
  • Makefiles and some variant of make utility (gnu make for linux and cygwin, nmake for windows's MSVC; you can start from gnu make manual: http://www.gnu.org/software/make/manual/make.html#Simple-Makefile then "2.4 Variables Make Makefiles Simpler" then "4.3 Types of Prerequisites" - with example of addprefix command to ask make find objects in some objdir. There is also VPATH special variable which instructs make to search sources and objects in several directories; but all objects in project should have different names).
  • Some IDE with support of Unix projects (Code::Blocks, list1, list2, wikilist of C/C++ IDE?); they usually manage all sources of you project and are able to generate Makefiles for project.
  • Some more modern build system, not the make: like CMake, SCons (or qmake if you use Qt).
Community
  • 1
  • 1
osgx
  • 90,338
  • 53
  • 357
  • 513
  • Thanks a lot - incredibly detailed answer! I've got a makefile now, I'll take a look at that `addprefix` command later today and also combining sections of my project into libraries :) – Kvothe Mar 13 '15 at 14:37