0

In the old Makefile there is an include part:

SHELL=/bin/sh

CORE_SRC=\
    ./Core/allocator.cpp \
    ./Core/etc... \
CORE_OBJS=$(CORE_SRC:.cpp=.o)

INCLUDE=\
`pkg-config --cflags glib-2.0 libpng` \
`sdl-config --cflags` \
`freetype-config --cflags` \
`./python-config-linux.sh --cflags` \
-I./TopLayer -I./etc...

CC=g++-4.4
CFLAGS=-O3 -pipe -Wall -fPIC -D__STDC_CONSTANT_MACROS

CORE_LFLAGS=\
-fPIC \
-Wl,-rpath,./libs

CORE_LDLIBS=\
`pkg-config --libs glib-2.0 libpng` \
`sdl-config --libs` -lz -ljpeg \
`freetype-config --libs` \
`curl-config --libs` \
-L./$(LIBSDIR) \
-letc...

GAME_LFLAGS=\
-shared -pthread -fPIC \
-Wl,-rpath,../libs

GAME_LDLIBS=\
-lm \
`python-config-linux.sh --libs`

target_name: $(CORE_OBJS) $(CORE_NAME)
target_name: override CFLAGS += -DAV_OUTPUT

$(CORE_NAME): $(CORE_OBJS)
        $(CC) $(CORE_LFLAGS) $(CORE_OBJS) $(CORE_LDLIBS) -o $@

.c.o:
        $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $*.o
.cpp.o:
        $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@

The execution of those configs is:

$pkg-config --cflags glib-2.0 libpng
->  -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/libpng12
$sdl-config --cflags
->  -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
$freetype-config --cflags
->  -I/usr/include/freetype2
$./python-config-linux.sh --cflags
->  -I/python-2.7.10/include/python2.7 -I/python-2.7.10/include/python2.7 -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes

I don't understand and a bit confused about Cmake - should I use target_include_dirictories() or find_packages()? Should target_include_dirictories() be used also with --cflags and libs like glib-2.0 libpng for pkg-config?

*Updated makefile. Removed unnecessary targets etc.

IgorZ
  • 1,125
  • 3
  • 20
  • 44
  • 1
    You have **different problems** and are trying to mix them together. Do not do that. Do you want to incorporate `pkg-config` into CMake? [That question](https://stackoverflow.com/questions/29191855/what-is-the-proper-way-to-use-pkg-config-from-cmake) describes how to do that. Want to do similar things for `sdl-config`? There are several question on this topic. E.g. [that one](https://stackoverflow.com/questions/3792327/cmake-sdl-config) suggests (among other things) to use `find_package(SDL)`. And so on. Please, ask about **single problem** in the question post. – Tsyvarev Aug 10 '18 at 15:52
  • Than you for your answere. I thought that there should be the same ways for any kinds of the configs. – IgorZ Aug 10 '18 at 18:30

1 Answers1

1
  • You will need both find_package() and target_link_libraries().
  • target_include_directories() is almost always obsolete when using libraries found through find_package. Docs.

Example

Example tested on macOS HighSierra with CMake 3.12.

project(Example)
cmake_minimum_required(VERSION 3.10)
find_package(PNG REQUIRED)

add_executable(prog main.c)  # main.c from: http://zarb.org/~gc/html/libpng.html
target_link_libraries(prog PNG)
# On Ubuntu 14.04 + CMake 3.2 use the older syntax (FindPNG.cmake is too old) from the next line:
# target_link_libraries(prog ${PNG_LIBRARIES})
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
  • I still don't get it. Should I use just a find_package("python-config-linux.sh") to replace "python-config-linux.sh --cflags"? – IgorZ Aug 10 '18 at 08:59
  • The argument to `find_package(PNG)` is (mostly) using a CMake program located in `share/.../Modules/FindPNG.cmake`. As such a shell script will not work. May I ask you to update your question to include all relevant information and a complete but minimal example of what works in Make. – Unapiedra Aug 10 '18 at 14:34
  • Made an update. Left only one target as an example (if I got it right). Thank you for your reply. – IgorZ Aug 10 '18 at 19:16