multiple C++ compiled to one object  
Author Message
Sleekit





PostPosted: Fri Oct 22 11:28:55 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

Hi ,
I have multiple C++ files to compile. How can I compile all of them to one
object or directly create library out of this C++ files.

e.g.
cl -c /Fotemp.obj src/*.cpp

command gives me error
Command line error D2036 : '/Ftemp.obj' not allowed with multiple source
files

How can I compiler all these C++ files to one object? or any better solution
so I create a library out of C++ files, without writting their names, by just
using src/*.cpp.

Thanks,
Avin Patel

Visual Studio305  
 
 
Igor





PostPosted: Fri Oct 22 11:28:55 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

> I have multiple C++ files to compile. How can I compile all of them
> to one object or directly create library out of this C++ files.
>
> e.g.
> cl -c /Fotemp.obj src/*.cpp
>
> command gives me error
> Command line error D2036 : '/Ftemp.obj' not allowed with multiple
> source files
>
> How can I compiler all these C++ files to one object? or any better
> solution so I create a library out of C++ files, without writting
> their names, by just using src/*.cpp.

Write a simple script that generates a text file with #include "x.cpp"
for each source file, then pass that to the compiler. Or, write a simple
script that compiles each file in the directory individually, then runs
lib.exe to collect them into a static library. I believe, but am not
sure, that lib.exe supports wildcards. Even if it doesn't, it takes a
command file, so the same script can generate one that lists all .obj
files to be included.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


 
 
Victor





PostPosted: Fri Oct 22 11:33:26 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object
> I have multiple C++ files to compile. How can I compile all of them to one
> object or directly create library out of this C++ files.
>
> e.g.
> cl -c /Fotemp.obj src/*.cpp
>
> command gives me error
> Command line error D2036 : '/Ftemp.obj' not allowed with multiple source
> files
>
> How can I compiler all these C++ files to one object? or any better solution
> so I create a library out of C++ files, without writting their names, by just
> using src/*.cpp.


Create another source file and #include all others there.

V
 
 
AvinPatel





PostPosted: Fri Oct 22 11:41:51 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object Hi Igor,
Yes I know that, I don't want to write any script. That is the way I am
doing right now.

But I want Cl compiler option, that can produce single object out of
multiple C++ file. or any better compiler option that can directly create the
library.

I don't want to write any script or .mk or .inc file.

Thanks for reply.
Avin Patel






> > I have multiple C++ files to compile. How can I compile all of them
> > to one object or directly create library out of this C++ files.
> >
> > e.g.
> > cl -c /Fotemp.obj src/*.cpp
> >
> > command gives me error
> > Command line error D2036 : '/Ftemp.obj' not allowed with multiple
> > source files
> >
> > How can I compiler all these C++ files to one object? or any better
> > solution so I create a library out of C++ files, without writting
> > their names, by just using src/*.cpp.
>
> Write a simple script that generates a text file with #include "x.cpp"
> for each source file, then pass that to the compiler. Or, write a simple
> script that compiles each file in the directory individually, then runs
> lib.exe to collect them into a static library. I believe, but am not
> sure, that lib.exe supports wildcards. Even if it doesn't, it takes a
> command file, so the same script can generate one that lists all .obj
> files to be included.
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>
>
 
 
Igor





PostPosted: Fri Oct 22 11:51:52 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

> Yes I know that, I don't want to write any script. That is the way I
> am doing right now.
>
> But I want Cl compiler option, that can produce single object out of
> multiple C++ file. or any better compiler option that can directly
> create the library.

There are none. You'll have to learn to live with what you have.

> I don't want to write any script or .mk or .inc file.

What's wrong with writing one?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


 
 
AvinPatel





PostPosted: Fri Oct 22 13:45:02 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object Hi Igor,
If I have to learn with what I have, than I don't need Visual C++ .Net. I
was fine with Visual C++ 4.0 or even without computer/Visual C++ in
IndianAge/IceAge.

So if you don't know the answer, than other can answer it or can improve
next release.

Regards,
Avin Patel









> > Yes I know that, I don't want to write any script. That is the way I
> > am doing right now.
> >
> > But I want Cl compiler option, that can produce single object out of
> > multiple C++ file. or any better compiler option that can directly
> > create the library.
>
> There are none. You'll have to learn to live with what you have.
>
> > I don't want to write any script or .mk or .inc file.
>
> What's wrong with writing one?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>
>
 
 
Victor





PostPosted: Fri Oct 22 14:47:27 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object
> [..]
> So if you don't know the answer, than other can answer it

Igor knows the answer. The answer is "there is no such CL option".

> or can improve
> next release.

Nobody is going to do improve the next release to accommodate your whim.
It's not how things are done. Our world is such that there is gravity
that affects everybody. You cannot blame anyone who points it out to
you, or sit and hope that in the next release of the universe there will
be no gravity. Wait... OTOH, why not? Just sit and wait till they
release a version of the compiler that does what you want. Sure thing.
Hold your breath as well, while you're at it.
 
 
Jochen





PostPosted: Fri Oct 22 14:53:28 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object Hi=?Utf-8?B?QXZpbiBQYXRlbA==?=,

> How can I compiler all these C++ files to one object? or any better
> solution so I create a library out of C++ files, without writting
> their names, by just using src/*.cpp.

Download VS2005(Beta1 Refresh) and use MSBuild (or wait until the RTM is
available).
Here you can specify wildcars for your sources, therefor you do not need to
write down the names of the source-files.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
 
Liviu





PostPosted: Fri Oct 22 15:09:42 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

> Hi ,
> I have multiple C++ files to compile. How can I compile all of them to one
> object or directly create library out of this C++ files.
>
> e.g.
> cl -c /Fotemp.obj src/*.cpp
>
> command gives me error
> Command line error D2036 : '/Ftemp.obj' not allowed with multiple source
> files

The compiler is correct. If this somehow worked and you had just 2 files...

file1.cpp
static int n = 0;

file2.cpp
static int n = 1;

...then what would you expect to happen?

(a) Receive a compiler error - then see below.

(b) Something else - then you need to review the definition of a compiler
"translation unit".

> How can I compiler all these C++ files to one object?

copy *.cpp temp.cpp
cl /c temp.cpp
del temp.cpp

> or any better solution
> so I create a library out of C++ files, without writting their names, by
> just using src/*.cpp.

It's not clear what you are trying to acomplish. A library is quite
different from an object. You can create a .lib with multiple files
compiled into different .obj's along the line Igor suggested.

> Thanks,
> Avin Patel

Liviu


 
 
Victor





PostPosted: Fri Oct 22 15:17:29 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object
> Hi=?Utf-8?B?QXZpbiBQYXRlbA==?=,
>
>
>>How can I compiler all these C++ files to one object? or any better
>>solution so I create a library out of C++ files, without writting
>>their names, by just using src/*.cpp.
>
>
> Download VS2005(Beta1 Refresh) and use MSBuild (or wait until the RTM is
> available).
> Here you can specify wildcars for your sources, therefor you do not need to
> write down the names of the source-files.

So, how to create a library from a directory in a single command-line?
 
 
Jochen





PostPosted: Fri Oct 22 15:25:44 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object HiVictor Bazarov,

>>>How can I compiler all these C++ files to one object? or any better
>>>solution so I create a library out of C++ files, without writting
>>>their names, by just using src/*.cpp.
>>
>>
>> Download VS2005(Beta1 Refresh) and use MSBuild (or wait until the RTM
>> is available).
>> Here you can specify wildcars for your sources, therefor you do not
>> need to write down the names of the source-files.
>
> So, how to create a library from a directory in a single command-line?

What problem do you have?

The question was not "single command-line" it was about "not to specify the
filenames".


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
 
Victor





PostPosted: Fri Oct 22 16:01:34 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object
> HiVictor Bazarov,
>
>
>>>>How can I compiler all these C++ files to one object? or any better
>>>>solution so I create a library out of C++ files, without writting
>>>>their names, by just using src/*.cpp.
>>>
>>>
>>>Download VS2005(Beta1 Refresh) and use MSBuild (or wait until the RTM
>>>is available).
>>>Here you can specify wildcars for your sources, therefor you do not
>>>need to write down the names of the source-files.
>>
>>So, how to create a library from a directory in a single command-line?
>
>
> What problem do you have?

I don't have a problem. The original poster, however, had a problem.
You made a suggestion that may help to solve it. I am trying to get you
to actually finish your suggestion. So, how to compile many files into
a single object file or how to create a library out of C++ files, without
writing their names. Have anything of _substance_ to reply?

>
> The question was not "single command-line" it was about "not to specify the
> filenames".

Pay attention, will you? Just read the original post. It's right there
above your "Download VS2005" suggestion, you even quoted it. The question
is how to compile "all these C++ file to _one object_".

I can see now that if you have the files sitting in 'src' directory, you
can, using VS2005, give the command to compile them all. How? OK, maybe
there is a way. Then what? You will end up with a bunch of .obj files.
What is next? I am guessing you need to run a librarian (LIB) to collect
them into .lib file, no? Can it be done? What command line to enter?

V
 
 
Jochen





PostPosted: Fri Oct 22 16:50:03 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object HiVictor Bazarov,

>> The question was not "single command-line" it was about "not to
>> specify the filenames".
>
> Pay attention, will you? Just read the original post. It's right
> there above your "Download VS2005" suggestion, you even quoted it.
> The question is how to compile "all these C++ file to _one object_".

Ok, I quoted the wrong part... you also know that it is _not_possible to
make only _one_ object file.
But you can make _one_ LIB file which is also fine for the OP.


> I can see now that if you have the files sitting in 'src' directory,
> you can, using VS2005, give the command to compile them all. How?
> OK, maybe there is a way. Then what? You will end up with a bunch of
> .obj files. What is next? I am guessing you need to run a librarian
> (LIB) to collect them into .lib file, no? Can it be done? What
> command line to enter?

It should look something like:
<Project DefaultTargets = "MakeLib">
<ItemGroup>
<CodeFiles Include=".\*.cpp" Exclude=".\generated\*.cpp" />
</ItemGroup>
<Target Name="Compile" >

</Target>
<Target Name="MakeLib" DependsOnTargets="Compile">
<Exec Command="LIB .\*.obj /out:out.lib" >
</Target>
</Project>



But the new VC2005Beta1 *supports* wildcards!

So you can just do:

cl /c *.cpp
lib *.obj /out:my.lib


Normally the VC++ 2005 Express Edition also suppots this:

You can download it here:
http://lab.msdn.microsoft.com/express/visualc/default.aspx


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
 
Jochen





PostPosted: Sat Oct 23 03:35:38 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object Hi=?Utf-8?B?QXZpbiBQYXRlbA==?=,

> How can I compiler all these C++ files to one object?

No.

> or any better
> solution so I create a library out of C++ files, without writting
> their names, by just using src/*.cpp.

This is simple:

cl /c *.cpp
lib *.obj /out:my.lib


Works with VC2003 and later (maybe also with VC2002)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
 
Jeff





PostPosted: Sat Oct 23 12:43:09 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

> So, how to create a library from a directory in a single command-line?

FWIW...

CL /c src\*.cpp & LIB /OUT:src\slib.lib src\*.obj

--
Jeff Partch [VC++ MVP]


 
 
Jochen





PostPosted: Sat Oct 23 14:05:28 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object Hi Jeff Partch [MVP],

>> So, how to create a library from a directory in a single command-line?
>
> CL /c src\*.cpp & LIB /OUT:src\slib.lib src\*.obj

The OP should be aware, that the settings of the compiler should be the
same as the EXE/DLL which uses this LIB. This means especially:
- CRT-Version: (Debug/Release, static/DLL, multi/single-threaded)
- Char-Mode: Ansi/MBCS/Unicode)
- ATL/MFC-Version

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
 
Victor





PostPosted: Sat Oct 23 19:46:16 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object


>> So, how to create a library from a directory in a single command-line?
>
> FWIW...
>
> CL /c src\*.cpp & LIB /OUT:src\slib.lib src\*.obj

I'd recommend using && instead of & because then it won't attempt to
create a library if CL returns an error. IIRC.

V


 
 
Jeff





PostPosted: Sat Oct 23 20:57:23 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object




> >> So, how to create a library from a directory in a single command-line?
> >
> > FWIW...
> >
> > CL /c src\*.cpp & LIB /OUT:src\slib.lib src\*.obj
>
> I'd recommend using && instead of & because then it won't attempt to
> create a library if CL returns an error. IIRC.

Your IIRC == TRUE. :)
--
Jeff Partch [VC++ MVP]


 
 
benben





PostPosted: Mon Oct 25 07:53:05 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object I don't think I am sure why you want to combine multiple cpp sources into a
single object file...I think the compilation model we are using today is
perfectly okay for me, especially when the IDE kicks in to handle everything
else than our creativity. Further, most programmers bear in mind when they
compose a single cpp source file that the source will be used to produce one
and only one object file. Breaking this convention may introduce subtle
problems that we just try hard to eliminate everyday. Just to name a few,
naming conflicts, linkage conflicts, etc. I personally feel relieved with
the linker linking a number of object files into an executable unit.

But I would also want to know what benefits you are looking for. What
particular reason has caused you to compile multiple sources into a single
obj? Version control?

ben


> > [..]
> > So if you don't know the answer, than other can answer it
>
> Igor knows the answer. The answer is "there is no such CL option".
>
> > or can improve
> > next release.
>
> Nobody is going to do improve the next release to accommodate your whim.
> It's not how things are done. Our world is such that there is gravity
> that affects everybody. You cannot blame anyone who points it out to
> you, or sit and hope that in the next release of the universe there will
> be no gravity. Wait... OTOH, why not? Just sit and wait till they
> release a version of the compiler that does what you want. Sure thing.
> Hold your breath as well, while you're at it.


 
 
Doug





PostPosted: Mon Oct 25 12:59:36 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

>I don't think I am sure why you want to combine multiple cpp sources into a
>single object file...I think the compilation model we are using today is
>perfectly okay for me, especially when the IDE kicks in to handle everything
>else than our creativity. Further, most programmers bear in mind when they
>compose a single cpp source file that the source will be used to produce one
>and only one object file. Breaking this convention may introduce subtle
>problems that we just try hard to eliminate everyday. Just to name a few,
>naming conflicts, linkage conflicts, etc. I personally feel relieved with
>the linker linking a number of object files into an executable unit.
>
>But I would also want to know what benefits you are looking for. What
>particular reason has caused you to compile multiple sources into a single
>obj? Version control?

Here's one possibility, which is based in some real world experience.

Imagine the overhead to compile a single source file is Q sec, and this
applies to every source file. Now imagine each file in a set S of N files
adds R sec to the compilation time. So, the total compilation time for S is
N(Q+R). If R is small enough compared to Q, then the compilation time is NQ,
and we should be able to achieve a speed improvement by merging multiple
files into one. Because R << Q, it takes little more time to compile a
composite of (say) 10 files than it does to compile one of the files
individually. So by using a composite file to feed the compiler larger
chunks of source code, we get a compile time of Q+X for S, where X is some
small number (nominally 10R) compared to the original N(Q+R).

Consequences:

1. Full build times drop dramatically.
2. Single file build times don't increase significantly, provided R is about
the same for each of the individual source files. (That is, R is not 100x
bigger for x.cpp than it is for y.cpp, so it doesn't hurt to compile the
composite xy.cpp vs either x.cpp or y.cpp.)

Unfortunately, because of the concerns you mentioned, this is at best very
difficult to automate, so it has limited applicability. For the real world,
special case mentioned earlier, it was implemented as Igor described, namely
by creating a number of composite files that used #include "x.cpp" for
multiple source files, and care was taken to avoid conflicts between
file-level declarations, e.g. anonymous namespaces, file-level statics,
macros, etc.

--
Doug Harrison
Microsoft MVP - Visual C++
 
 
benben





PostPosted: Thu Oct 28 07:10:53 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object I am not quite sure about that idea because I think even the preprocessor
needs sometime to deal with the #include directives. Logically, this
marginalizes the build time difference. I have never built any big projects
with multiple source files into a single object file and I have a bit doubt
on how "dramatic" the build time will drop should the multiple source
compilation be applied



>
> >I don't think I am sure why you want to combine multiple cpp sources into
a
> >single object file...I think the compilation model we are using today is
> >perfectly okay for me, especially when the IDE kicks in to handle
everything
> >else than our creativity. Further, most programmers bear in mind when
they
> >compose a single cpp source file that the source will be used to produce
one
> >and only one object file. Breaking this convention may introduce subtle
> >problems that we just try hard to eliminate everyday. Just to name a few,
> >naming conflicts, linkage conflicts, etc. I personally feel relieved with
> >the linker linking a number of object files into an executable unit.
> >
> >But I would also want to know what benefits you are looking for. What
> >particular reason has caused you to compile multiple sources into a
single
> >obj? Version control?
>
> Here's one possibility, which is based in some real world experience.
>
> Imagine the overhead to compile a single source file is Q sec, and this
> applies to every source file. Now imagine each file in a set S of N files
> adds R sec to the compilation time. So, the total compilation time for S
is
> N(Q+R). If R is small enough compared to Q, then the compilation time is
NQ,
> and we should be able to achieve a speed improvement by merging multiple
> files into one. Because R << Q, it takes little more time to compile a
> composite of (say) 10 files than it does to compile one of the files
> individually. So by using a composite file to feed the compiler larger
> chunks of source code, we get a compile time of Q+X for S, where X is some
> small number (nominally 10R) compared to the original N(Q+R).
>
> Consequences:
>
> 1. Full build times drop dramatically.
> 2. Single file build times don't increase significantly, provided R is
about
> the same for each of the individual source files. (That is, R is not 100x
> bigger for x.cpp than it is for y.cpp, so it doesn't hurt to compile the
> composite xy.cpp vs either x.cpp or y.cpp.)
>
> Unfortunately, because of the concerns you mentioned, this is at best very
> difficult to automate, so it has limited applicability. For the real
world,
> special case mentioned earlier, it was implemented as Igor described,
namely
> by creating a number of composite files that used #include "x.cpp" for
> multiple source files, and care was taken to avoid conflicts between
> file-level declarations, e.g. anonymous namespaces, file-level statics,
> macros, etc.
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++


 
 
Doug





PostPosted: Thu Oct 28 11:35:13 CDT 2004 Top

Visual Studio C++ >> multiple C++ compiled to one object

>I am not quite sure about that idea because I think even the preprocessor
>needs sometime to deal with the #include directives. Logically, this
>marginalizes the build time difference.

It is the preprocessor that replaces #include directives with file contents,
so I don't get what you mean.

>I have never built any big projects
>with multiple source files into a single object file and I have a bit doubt
>on how "dramatic" the build time will drop should the multiple source
>compilation be applied

In the real world case I'm familiar with, using probably VC2-4 circa 1996,
it cut the "Rebuild All" time by more than half without significantly
affecting ordinary builds.

In my last message, I somewhat abstractly described the conditions under
which this can work. To put it more concretely, you need:

1. A machine slow enough to notice the time it takes to compile each source
file.
2. Significant per-source file overhead, which can accompany large
precompiled header files that contain a lot of templates.
3. Source files whose contents don't add much time to the per-file overhead.
4. Source files that are amenable to being "merged" using #include as
described in my last message.

Here's a concrete example from a post a couple of years ago:

*****

Suppose you're writing a Windows library. Every file needs to #include
<windows.h>. Precompiled headers are not available. Your files look like:

f1.cpp:

#include <windows.h>

void f1() {}

f2.cpp:

#include <windows.h>

void f2() {}

f3.cpp:

#include <windows.h>

void f3() {}

Using VC6 on an old 200 MHz machine, the command:

cl -c f1.cpp

takes 5 sec. The command:

cl -c f1.cpp f2.cpp f3.cpp

takes 16 sec.

Now merge f2.cpp and f3.cpp into f1.cpp, so we have:

#include <windows.h>

void f1() {}
void f2() {}
void f3() {}

Unsurprisingly, the command:

cl -c f1.cpp

still takes 5 sec. This result holds as the functions grow to hundreds
of lines each (and actually use the Windows API), depending on the
complexity of the code, of course. Note that if only two of these
files needed to be compiled due to some (unshown) header they depend
on changing, it would still be a big win to compile the composite f1
instead of compiling them individually. And if only one of them needed
to be compiled, you don't lose anything by compiling the composite
file. This is what I mean by the per-file overhead dominating the
compilation time. When this is the case, it may be no more expensive
to compile many hundreds of lines than it is to compile one line, up
to a point. As I've said, I've observed a similar phenomenon when
using large precompiled headers and templates, but that's not so easy
to demonstrate in a newsgroup post. However, it's what initially led
me to look at this, roughly seven years ago.

*****

--
Doug Harrison
Microsoft MVP - Visual C++