title: Compile linker script in cmake - cannot find object file (embedded project)
body: <p>My problem is that I want to do a cmake file from normal makefile from this project:
  <a href="https://github.com/brianwiddas/pi-baremetal" rel="nofollow">https://github.com/brianwiddas/pi-baremetal</a></p>
  
  <p>I create CMakeLists file, but I'm having trouble with linking the project. After make command I have an error like this:</p>
  
  <pre><code>Linking C executable rpi-0.1.elf
  /usr/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld: cannot find start.o
  collect2: error: ld returned 1 exit status
  make[2]: *** [rpi-0.1.elf] Błąd 1
  make[1]: *** [CMakeFiles/rpi.dir/all] Błąd 2
  </code></pre>
  
  <p>Linker code fragment:</p>
  
  <pre><code>MEMORY
  {
      initsys : org = 0x8000, len = 1M
      kernel : org = 0xf0000000, len = 1M
      data : org = 0xc0000000, len = 1M
  }
  SECTIONS
  {
      /* Initial section, loads/runs at 0x8000 */
      .init : {
          start.o(.text* .data* .bss* .rodata*)
          initsys.o (.text* .data* .bss* .rodata*)
      } &gt;initsys
  
      //the rest of code in SECTION
  
  }
  </code></pre>
  
  <p>I realised that cmake create object file with ".c.obj/.s.obj" extension, compare with this makefile with ".o" extension, I change it in linker code, but now the error is:</p>
  
  <pre><code>cannot find start.s.obj
  </code></pre>
  
  <p>Now I dont know what is wrong. Maybe because in makefile project the source code file and object file is in the same folder. Cmake create object file in another folder, in my case:</p>
  
  <pre><code>source files folder - ../Projekt/code
  object files folder - ../Projekt/build/CMakeFiles/rpi.dir/code
  </code></pre>
  
  <p>Maybe the linker script do not see the object files, because they are in another folder.
  The build folder is created automatically by cmake.</p>
  
  <p>This is my <strong>CMakeLists.txt</strong> file:</p>
  
  <pre><code>## Minimal cmake version
  cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
  
  ## Project name
  project(rpi C CXX ASM)
  
  ## Project version
  set(${PROJECT_NAME}_version "0.1")
  
  ## Project options
  option(DISABLE_NEWLIB_SUPPLIED_SYSCALLS "Disables pre-defined libraries for ARM" ON)
  
  ## Compilation options
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O6 -nostdinc -ffreestanding -marm -mcpu=arm1176jzf-s")
  
  ## Creation of the necessary definitions
  if (DISABLE_NEWLIB_SUPPLIED_SYSCALLS)
      add_definitions(-DDISABLE_NEWLIB_SUPPLIED_SYSCALLS)
  endif (DISABLE_NEWLIB_SUPPLIED_SYSCALLS)
  
  ## Assembler source code
  set(ASM_SOURCE_FILES ${PROJECT_SOURCE_DIR}/code/start.s)
  
  ## C source code
  set(C_SOURCE_FILES ${PROJECT_SOURCE_DIR}/code/atags.c
             ${PROJECT_SOURCE_DIR}/code/atags.h
             ${PROJECT_SOURCE_DIR}/code/barrier.h
             ${PROJECT_SOURCE_DIR}/code/divby0.c
             ${PROJECT_SOURCE_DIR}/code/framebuffer.c
             ${PROJECT_SOURCE_DIR}/code/framebuffer.h
             ${PROJECT_SOURCE_DIR}/code/interrupts.c
             ${PROJECT_SOURCE_DIR}/code/interrupts.h
             ${PROJECT_SOURCE_DIR}/code/led.c
             ${PROJECT_SOURCE_DIR}/code/led.h
             ${PROJECT_SOURCE_DIR}/code/initsys.c
             ${PROJECT_SOURCE_DIR}/code/mailbox.c
             ${PROJECT_SOURCE_DIR}/code/mailbox.h
             ${PROJECT_SOURCE_DIR}/code/main.c
             ${PROJECT_SOURCE_DIR}/code/memory.c
             ${PROJECT_SOURCE_DIR}/code/memory.h
             ${PROJECT_SOURCE_DIR}/code/memutils.c
             ${PROJECT_SOURCE_DIR}/code/memutils.h
             ${PROJECT_SOURCE_DIR}/code/teletext.h
             ${PROJECT_SOURCE_DIR}/code/textutils.c
             ${PROJECT_SOURCE_DIR}/code/textutils.h)
  
  ## Additional files
  set(OTHER_FILES ${PROJECT_SOURCE_DIR}/code/linker.ld)
  
  ## Create an executable binary files
  add_executable(${PROJECT_NAME} ${ASM_SOURCE_FILES} ${C_SOURCE_FILES})
  
  ## Settings resulting executable file
  set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}-${${PROJECT_NAME}_version}.elf")
  set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-Map,${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.map -T ${PROJECT_SOURCE_DIR}/code/linker.ld -nostdlib -nostartfiles -gc-sections")
  
  ## Creates a kernel image
  add_custom_command(TARGET ${PROJECT_NAME} COMMAND ${CROSS_COMPILER_PREFIX}objcopy ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.elf -O binary ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${${PROJECT_NAME}_version}.img)
  </code></pre>
  
  <p>and <strong>toolchain-raspberry-pi.cmake</strong> file:</p>
  
  <pre><code>## Determination of the operating system on which the program is designed
  set(CMAKE_SYSTEM_NAME Generic)
  
  ## The term architecture on which the program is established
  set(${PROJECT_NAME}_architecture raspberry-pi)
  
  ## Prefix tools used for cross-compiling
  set(CROSS_COMPILER_PREFIX arm-none-eabi-)
  
  ## Compilers that will be used.
  include (CMakeForceCompiler)
  cmake_force_c_compiler(${CROSS_COMPILER_PREFIX}gcc GNU)
  cmake_force_cxx_compiler(${CROSS_COMPILER_PREFIX}g++ GNU)
  </code></pre>
  
  <p>How to fix this error and why cmake do not create object files with ".o" extensions and in the same folder as source code?
  Please help.</p>
  
  <h1><strong>Solution:</strong></h1>
  
  <p>In linker script I add * before start.s.obj/initsys.c.obj like this:</p>
  
  <pre><code>*start.s.obj (.text* .data* .bss* .rodata*)
  *initsys.c.obj (.text* .data* .bss* .rodata*)
  </code></pre>