Create executable and shared library with one makefile
up vote
3
down vote
favorite
Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?
compiling make executable shared-library
New contributor
add a comment |
up vote
3
down vote
favorite
Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?
compiling make executable shared-library
New contributor
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?
compiling make executable shared-library
New contributor
Using makefiles is new to me. My first tests with the simplest approach worked very well so far. But now I got stuck writing a makefile that creates a executable file and a shared library. As I said I'm new to makefiles. Here is my simple approach:
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
When the makefile is executed only the executable will be compiled. Is it even possible to create two objects with one makefile? What is the best approach to create these files?
compiling make executable shared-library
compiling make executable shared-library
New contributor
New contributor
edited 2 days ago
Peter Mortensen
1,03821016
1,03821016
New contributor
asked 2 days ago
Olupo
185
185
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
By default make
will build only the first target in a Makefile
which is exe.a
in your case. You can either tell make
to create
another target:
make lib.so
or both targets:
make lib.so exe.a
or (preferred) introduce a new first target which is usually named
all
:
all: exe.a lib.so
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
Now a simple make
will build the (pseudo-) target all
which in turn
depends on exe.a
and lib.so
, so they get built first. Note that you
must not have a file called all
in your directory in this case because
then make
gets confused.
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter thisall
target very often.
– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
By default make
will build only the first target in a Makefile
which is exe.a
in your case. You can either tell make
to create
another target:
make lib.so
or both targets:
make lib.so exe.a
or (preferred) introduce a new first target which is usually named
all
:
all: exe.a lib.so
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
Now a simple make
will build the (pseudo-) target all
which in turn
depends on exe.a
and lib.so
, so they get built first. Note that you
must not have a file called all
in your directory in this case because
then make
gets confused.
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter thisall
target very often.
– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
add a comment |
up vote
4
down vote
accepted
By default make
will build only the first target in a Makefile
which is exe.a
in your case. You can either tell make
to create
another target:
make lib.so
or both targets:
make lib.so exe.a
or (preferred) introduce a new first target which is usually named
all
:
all: exe.a lib.so
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
Now a simple make
will build the (pseudo-) target all
which in turn
depends on exe.a
and lib.so
, so they get built first. Note that you
must not have a file called all
in your directory in this case because
then make
gets confused.
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter thisall
target very often.
– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
By default make
will build only the first target in a Makefile
which is exe.a
in your case. You can either tell make
to create
another target:
make lib.so
or both targets:
make lib.so exe.a
or (preferred) introduce a new first target which is usually named
all
:
all: exe.a lib.so
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
Now a simple make
will build the (pseudo-) target all
which in turn
depends on exe.a
and lib.so
, so they get built first. Note that you
must not have a file called all
in your directory in this case because
then make
gets confused.
By default make
will build only the first target in a Makefile
which is exe.a
in your case. You can either tell make
to create
another target:
make lib.so
or both targets:
make lib.so exe.a
or (preferred) introduce a new first target which is usually named
all
:
all: exe.a lib.so
exe.a: main.c func.c
gcc main.c func.c -o exe.a
lib.so: func.c
gcc func.c -o lib.so -fPIC -shared
Now a simple make
will build the (pseudo-) target all
which in turn
depends on exe.a
and lib.so
, so they get built first. Note that you
must not have a file called all
in your directory in this case because
then make
gets confused.
answered 2 days ago
PerlDuck
4,83111130
4,83111130
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter thisall
target very often.
– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
add a comment |
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter thisall
target very often.
– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
Thank you. The option with the new first target is exactly what I was looking for.
– Olupo
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter this
all
target very often.– PerlDuck
2 days ago
@Olupo You are welcome. When you study other Makefiles you'll encounter this
all
target very often.– PerlDuck
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
I've read some other makefiles, but most of them were to advanced for my current skill level.
– Olupo
2 days ago
2
2
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
I know exactly what you mean :-) My skills aren't very good either and when it comes to more complicated Makefiles I usually think of them as rocket science.
– PerlDuck
2 days ago
add a comment |
Olupo is a new contributor. Be nice, and check out our Code of Conduct.
Olupo is a new contributor. Be nice, and check out our Code of Conduct.
Olupo is a new contributor. Be nice, and check out our Code of Conduct.
Olupo is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1095317%2fcreate-executable-and-shared-library-with-one-makefile%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown