1

linking errors. Hellow. I have a project with eight .c and eight .h files. How should i write CMakeLists.txt to solve include dependences for all of them? Creating libraries isn't the way, because every file depends on some others. Thank you!

My CMakeLists.txt was like

  cmake_minimum_required(VERSION 2.8)
  project(calc-2.4.0)
  set(SOURCE_EXE main.c expmath.c interface.c container.c
  polnot.c error.c getelem.c dbg.c
  buildctrl.h expmath.h interface.h container.h
  polnot.h error.h getelem.h dbg.h)
  add_definitions(-Wall -O2)
  add_executable(calc ${SOURCE_EXE})

and

cmake_minimum_required(VERSION 2.8)
project(calc-2.4.0)
set(SOURCE_EXE main.c expmath.c interface.c container.c
polnot.c error.c getelem.c dbg.c)
add_definitions(-Wall -O2)
add_executable(calc ${SOURCE_EXE})

So linter can't see definitions anyway.

8
  • 1
    Are all source files supposed to generate a single or multiple executable programs? And what have you tried? How did your attempt work or not work? Please read about how to ask good questions, and learn how to create a Minimal, Complete, and Verifiable Example. Commented Apr 29, 2018 at 10:28
  • I used netbeans for long time and now I want to switch to atom. This project successfully compiled by netbeans. And now I try to generate compile_commands.json for atom linter. Commented Apr 29, 2018 at 11:33
  • 1
    Both those CMakeLists.txt files are fine, and should create a build-system that will build your calc program. Commented Apr 29, 2018 at 12:26
  • What error are you getting? That looks fine from what I can see. Commented Apr 29, 2018 at 12:35
  • 1
    Is . in your include paths list in your configuration page (or in a .gcc-flags.json file—see the File/Project-Specific Settings section of the linter-gcc docs)? Commented Apr 29, 2018 at 12:46

1 Answer 1

1

It is not necessary to add all header files to your CMakeLists.txt. It should be sufficient to add the folders the header files are in. The required cmake command is include_directories.

My recommendation is the following:

  • Add all *.cpp and header only files
  • Add all required header files using include_directory

Example:

  cmake_minimum_required(VERSION 2.8)
  project(calc-2.4.0)
  set(SOURCE_EXE main.c expmath.c interface.c container.c
  polnot.c error.c getelem.c dbg.c
  buildctrl.h
  )
  add_definitions(-Wall -O2)
  include_directory(${CMAKE_CURRENT_SOURCE_DIR})
  add_executable(calc ${SOURCE_EXE})
  • CMAKE_CURRENT_SOURCE_DIR points to the current source dir
  • I recommend to move the sources into a subdirectory of the folder your top CMakeLists.txt file is in. See Project Structure
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.