I'm using the -c option with g++ to create a bunch of object files, and it's only letting me specify one source file for each object file. I want to have multiple files go into some of them. Is there any way to do this?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Do the Java Integer and Double objects have unnece
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You can use
ld -r
to combine the objects while keeping relocation information and leaving constructors unresolved:We use -o option to create an object file...... By Default when we compile file using gcc or g++ we get object file named as a.out but we can change its name.... use following to compile file
gcc Filename.c -o NewFilename.out
then to run file you can use ./NewFilename.out
Using the solution from Peter Alexander is the main one that comes to mind.
But, keep in mind that by using this method, you'll have to compile your whole sources files each time. When your project grows bigger, compilation time can become a pain.
Furthermore, compiling several files on their own enable the use of the various cores on modern CPUs: each source file will be compiled in its own process, at full speed. Do not under-use the power of the multi cores.
You can create
archive
which is a set of object files.So effectively you have combined
file1.cpp
andfile2.cpp
intomylib.a
.I know you are asking about how to combine .cpp files into one object file. I assume your goal for doing this is to link the combined object at a later time with other individual object files. If this is the case you may be able to use a static or dynamic library instead. For your goals I suggest the dynamic library since you can do a single compile and link step skipping the generation of object files altogether. Using a command such as
g++ -shared -fpic file1.cpp file2.cpp file3.cpp -o libtest.so
you can combine your .cpp files that need combining into libraries. To compile the library(s) with your individual object files use a command such asg++ -ltest individual1.o individual2.o individual3.o -o myexecutable
. In that final linking step I assumelibtest.so
is in your current directory. If its not in your current directory add a-L
flag and the directorylibtest.so
is in.The only solution I know of is to create a separate C++ file, which includes all of the files you want to compile, and compile that. A pretty bad solution, in my mind; generally, you want to increase the granularity of the object files, not reduce it.
The real question, I suppose, is what are you trying to achieve. Why do you want only a single object file?