I want to know if there is a way to compile dynamically using a makefile. Assume I have a.cpp
, b.cpp
, and c.cpp
files. These are some algorithms that I want to compare their runtime. First, I only want to compile a and b file and then execute to see the result. Next, I want to compile b and c and run.
It is not just an order, a and b can be compiled or b and c, or a and c or (a, b and c) altogether.
Can I do this using the makefile ?
Not sure how you want to "compare" runtimes, so this example just prints them via the time
command, but you could use awk and gnuplot to generate plots if you want. Each target simply specifies a shell command to run. The %
is used to define a target based on patterns.
all: ab bc
%.out: %.cpp
gcc $< -o $@
ab: a.out b.out
time ./a.out && time ./b.out
bc: b.out c.out
time ./b.out && time ./c.out
(make sure indentation is tabs)
Yes, this is just a matter of dependency ordering: it would look something like:
all: results_ab results_bc
#compare results...
results_ab: a.o b.o
# link, and run, and store results
results_bc: results_ab b.o c.o
# link, run and store results.
While a compiler and makefile could potentially be worked to do this, Id suggest you do not do this. Restructure your program.
eg I am guessing you have like test_runner.cpp
, algorithm_a.cpp
and algorithm_b.cpp
?
eg make a template function taking the algorthim to test. Then use it for each of the implementations.
template<class T> void test_implementation() { ... do stuff with T to test it ... }
void main()
{
test_implementation<A>();
test_implementation<A>();
}
So firstly, what is so bad about making them into one program for your test? Then you can run and time the two algorithms sequentially, or you could use a command line parameter?
Alterntively, if you consider test_runner.cpp
as being a library, then your two algorthimns to test are seperate programs, just using that library.
Also take some time to learn the makefile syntax. Once you understand the syntax solutions should be obvious, allthough still be no means something you should apply to this situation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.