title: Convert Makefile into CMakeLists, where to start
body: <p>I searched on the inet but I did not find any clear answer. Could you point me in the right direction on how to convert a Makefile into a CMakeLists?</p>
  <p>I want to do that because I am new both to makefile and to cmake. In my job CMake is more used and since I need to start using one of them I prefer having everything in CMake. I know CMake is generating a Makefile but for me CMake is way easier to read than a Makefile.</p>
  <p>I have the following Makefile:</p>
  <pre><code>PREFIX ?= /usr/local
  
  CC = gcc
  AR = ar
  
  CFLAGS = -std=gnu99 -fPIC -Wall -Wno-unused-parameter -Wno-unused-function -I. -O4
  
  APRILTAG_SRCS := $(shell ls *.c common/*.c)
  APRILTAG_HEADERS := $(shell ls *.h common/*.h)
  APRILTAG_OBJS := $(APRILTAG_SRCS:%.c=%.o)
  TARGETS := libapriltag.a libapriltag.so 
  # LIBS := -Lusr/include/flycapture
  
  .PHONY: all
  all: $(TARGETS)
      @$(MAKE) -C example all
  
  .PHONY: install
  install: libapriltag.so
      @chmod +x install.sh
      @./install.sh $(PREFIX)/lib libapriltag.so #this should be the line that install the library
      @./install.sh $(PREFIX)/include/apriltag $(APRILTAG_HEADERS)
      @sed 's:^prefix=$$:prefix=$(PREFIX):' &lt; apriltag.pc.in &gt; apriltag.pc
      @./install.sh $(PREFIX)/lib/pkgconfig apriltag.pc
      @rm apriltag.pc
      @ldconfig
  
  libapriltag.a: $(APRILTAG_OBJS)
      @echo &quot;   [$@]&quot;
      @$(AR) -cq $@ $(APRILTAG_OBJS)
  
  libapriltag.so: $(APRILTAG_OBJS)
      @echo &quot;   [$@]&quot;
      @$(CC) -fPIC -shared -o $@ $^
  
  %.o: %.c
      @echo &quot;   $@&quot;
      @$(CC) -o $@ -c $&lt; $(CFLAGS)
  
  .PHONY: clean
  clean:
      @rm -rf *.o common/*.o $(TARGETS)
      @$(MAKE) -C example clean
  </code></pre>
  <p>I am not asking you to do my job but I would like to have some kind of guide or a good link where to look.</p>
  <p>The project contains both C and C++ programming languages.</p>
  <p>I started creating a new CMakeLists.txt file, but it is still not working. It gives me the following errors:
  You have called ADD_LIBRARY for library librapriltag.a without any source files. This typically indicates a problem with your CMakeLists.txt file</p>
  <pre><code>-- Configuring done
  CMake Error: Cannot determine link language for target &quot;librapriltag.a&quot;.
  CMake Error: CMake can not determine linker language for target: librapriltag.a
  -- Generating done
  -- Build files have been written to: .....
  </code></pre>
  <p>The CMakeLists.txt I started creating is the following:</p>
  <pre><code>project( apriltag2 C CXX)
  
  cmake_minimum_required(VERSION 2.8)
  
  set(CMAKE_C_FLAGS &quot;-std=gnu99 -fPIC -Wall -Wno-unused-parameter -Wno-unused-function -I. -O4&quot;)
  
  include_directories(&quot;/home/fschiano/Repositories/apriltag2&quot;)
  include_directories(&quot;/home/fschiano/Repositories/apriltag2/common&quot;)
  
  add_library( librapriltag.a )
  </code></pre>
  <p>The CMakeLists.txt which works is the following:</p>
  <pre><code>project( apriltag2 )
  
  cmake_minimum_required(VERSION 2.8)
  
  set(CMAKE_C_FLAGS &quot;-std=gnu99 -fPIC -Wall -Wno-unused-parameter -Wno-unused-function -I. -O4&quot;)
  
  message(&quot;CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}&quot;)
  
  file(GLOB apriltag_SRC &quot;*.c&quot;)
  file(GLOB apriltag_HEADERS &quot;*.h&quot;)
  
  set(APRILTAG_SRCS ${apriltag_SRC})
  set(APRILTAG_HEADERS ${apriltag_HEADERS})
  
  message(STATUS &quot;CMAKE_CURRENT_LIST_DIR=${CMAKE_CURRENT_LIST_DIR}&quot;)
  
  add_library(apriltag STATIC ${APRILTAG_SRCS})
  
  target_include_directories(apriltag PUBLIC ${CMAKE_SOURCE_DIR})
  target_compile_options(apriltag PUBLIC -fPIC -Wall -Wno-unused-parameter -Wno-unused-function -O4)
  
  install(TARGETS apriltag
          ARCHIVE DESTINATION lib
          RUNTIME DESTINATION bin
          LIBRARY DESTINATION lib)
  install(DIRECTORY CMAKE_CURRENT_LIST_DIR/include/
          DESTINATION CMAKE_CURRENT_LIST_DIR/include/
          FILES_MATCHING PATTERN *.h)
  </code></pre>
  <p><strong>EDIT:</strong></p>
  <p>Something is still not right. If I want to change something in my library, like something which is in <code>/home/fschiano/Repositories/apriltag2/common</code>
  If I use the Makefile which I had before doing all these modifications and I do:</p>
  <ol>
  <li><p>make</p>
  </li>
  <li><p>do some modifications in the files I wanted to modify</p>
  </li>
  <li><p>sudo make install, which would give me the following output:</p>
  <pre><code>/usr/local/lib/libapriltag.so
  /usr/local/include/apriltag/apriltag.h
  /usr/local/include/apriltag/common/g2d.h
  /usr/local/include/apriltag/common/getopt.h
  /usr/local/include/apriltag/common/homography.h
  /usr/local/include/apriltag/common/image_f32.h
  /usr/local/include/apriltag/common/image_u8.h
  /usr/local/include/apriltag/common/image_u8x3.h
  /usr/local/include/apriltag/common/matd.h
  /usr/local/include/apriltag/common/math_util.h
  /usr/local/include/apriltag/common/pnm.h
  /usr/local/include/apriltag/common/postscript_utils.h
  /usr/local/include/apriltag/common/string_util.h
  /usr/local/include/apriltag/common/svd22.h
  /usr/local/include/apriltag/common/thash_impl.h
  /usr/local/include/apriltag/common/timeprofile.h
  /usr/local/include/apriltag/common/time_util.h
  /usr/local/include/apriltag/common/unionfind.h
  /usr/local/include/apriltag/common/workerpool.h
  /usr/local/include/apriltag/common/zarray.h
  /usr/local/include/apriltag/common/zhash.h
  /usr/local/include/apriltag/common/zmaxheap.h
  /usr/local/include/apriltag/tag16h5.h
  /usr/local/include/apriltag/tag25h7.h
  /usr/local/include/apriltag/tag25h9.h
  /usr/local/include/apriltag/tag36artoolkit.h
  /usr/local/include/apriltag/tag36h10.h
  /usr/local/include/apriltag/tag36h11.h
  /usr/local/lib/pkgconfig/apriltag.pc
  /sbin/ldconfig.real: /usr/lib/libstdc++.so.5 is not a symbolic link
  
  </code></pre>
  </li>
  </ol>
  <p>and the modifications would take effect.</p>
  <pre><code></code></pre>
  <p>Now, if I remove the Makefile and I do:</p>
  <ol>
  <li><p>cmake .</p>
  </li>
  <li><p>make</p>
  </li>
  <li><p>do some modifications in the files I wanted to modify</p>
  </li>
  <li><p>sudo make install, it gives me the following output:</p>
  <p>Install the project...
  -- Install configuration: &quot;&quot;
  -- Up-to-date: /usr/local/lib/libapriltag.a</p>
  </li>
  </ol>
  <p><strong>So it seems that the install part of the CMakeLists.txt is not right!</strong></p>
  <p>The file install.sh is the following.</p>
  <pre><code>#!/bin/sh -e
  # Usage: install.sh TARGET [RELATIVE PATHS ...]
  #
  # e.g. ./install.sh /usr/local foo/file1 foo/file2 ...
  # This creates the files /usr/local/foo/file1 and /usr/local/foo/file2
  
  TARGETDIR=$1
  shift
  
  for src in &quot;$@&quot;; do
      dest=$TARGETDIR/$src
      mkdir -p $(dirname $dest)
      cp $src $dest
      echo $dest
  done
  </code></pre>
  <p>Could you try to help me?
  Thanks</p>