Issue
I have the following codes in mylib.pc.in
, and want to use the CMake command configure_file(mylib.pc.in mylib.pc)
to generate a correct mylib.pc
file which is expanded from mylib.pc.in
; but this doesn't work.
prefix=$ENV{HOME}/xcompiled-root/
exe_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include
Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
Requires:
Libs: -L@{libdir} -lpthread -lrt -ldl -lm
Cflags: -I@{includedir}
I just want the mylib.pc
be something like this:
prefix=/home/myusr/xcompiled-root/
exe_prefix=/home/myusr/xcompiled-root/
libdir=/home/myusr/xcompiled-root/lib
includedir=/home/myusr/xcompiled-root/include
...
but I get this:
prefix=/home/myusr/xcompiled-root/
exe_prefix=
libdir=/lib
includedir=/include
...
what should I do?!
Solution
Construction ${prefix}
is a variable reference both in CMake scripting and in .pc
file scripting. So you need to choose at which stage you want to expand this reference:
If you want CMake to expand this reference, you need to have a CMake variable named
prefix
when you useconfigure_file
:# File: CMakeLists.txt set(prefix "$ENV{HOME}/xcompiled-root/") configure_file(mylib.pc.in mylib.pc)
Note, that with such setting you no longer need to define
prefix
variable in.pc
file:# File: mylib.pc.in exe_prefix=${prefix} libdir=${prefix}/lib includedir=${prefix}/include
Resulted
mylib.pc
file will be:exe_prefix=/home/myusr/xcompiled-root/ libdir=/home/myusr/xcompiled-root/lib includedir=/home/myusr/xcompiled-root/include
If you want to expand variable reference
${prefix}
only when pkg-config is called, you need to prevent CMake to expand this reference. E.g. you may insert${}
construction, which expanded by CMake to nothing, between$
and{
characters:# File: mylib.pc.in prefix=$ENV{HOME}/xcompiled-root/ exe_prefix=$${}{prefix} libdir=$${}{prefix}/lib includedir=$${}{prefix}/include
Then, with common
configure_file
# File: CMakeLists.txt configure_file(mylib.pc.in mylib.pc)
you will have following
mylib.pc
file:prefix=/home/myusr/xcompiled-root/ exe_prefix=${prefix} libdir=${prefix}/lib includedir=${prefix}/include
When this file will be processed by
pkg-config
,${prefix}
will be expanded (internally) to/home/myusr/xcompiled-root/
.If you do not want to bother about escaping
${}
construction, use@ONLY
option forconfigure_file
. With that option CMake only expands@VAR@
but leaves${var}
unchanged:# File: CMakeLists.txt # Construction @VAR@ doesn't support reference to environment variable. # So prepare pure CMake variable which contains $ENV{HOME}. set(user_home "$ENV{HOME}") configure_file(mylib.pc.in mylib.pc @ONLY)
With
@ONLY
option you may simply use$
in the.in
:prefix=@user_home@/xcompiled-root/ exe_prefix=${prefix} libdir=${prefix}/lib includedir=${prefix}/include Name: @PROJECT_NAME@ Description: @PROJECT_DESCRIPTION@ Version: @PROJECT_VERSION@ Requires: Libs: -L@{libdir} -lpthread -lrt -ldl -lm Cflags: -I@{includedir}
Note, that CMake leaves unchanged
@{var}
construction too. This is because{
character is not valid for a variable's name in CMake.
Answered By - Tsyvarev Answer Checked By - Marie Seifert (WPSolving Admin)