4

Question is similar to this question

Handling header files dependencies with cmake

I have sample program dir having main.c main.h and CMakeLists.txt

main.h contents are

#ifndef MAIN_H
#define MAIN_H
int t=3;
int y=2;
#endif

main.c contents are

#include <main.h>
#include<stdio.h>
int main(){

  printf("%d apple",t);
}

and CMakeLists.txt

PROJECT( test )
AUX_SOURCE_DIRECTORY(. test_SRCS)
include_directories(.)
ADD_EXECUTABLE (main ${test_SRCS})

but cmake is not rebuilding main.c on modification of header file. I want it to auto-generate header file dependency. Is it possible using cmake ?

if not is there any other tool which can do that ?

Community
  • 1
  • 1
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186

3 Answers3

4

As mentioned in my comment, I have tried out your example and things were working fine: if main.h was modified then main.c would be recompiled.

My installation of CMake (version 2.8.0) told me to add

cmake_minimum_required(VERSION 2.8)

to the CMakeLists.txt file, but that is all of the adjustments I needed.

antonakos
  • 8,261
  • 2
  • 31
  • 34
1

Answering this for others that google search...

I ran into this problem with one of my projects. As it turns out I added the header to the cpp file after running cmake. Re-running cmake fixed the problem. If you run into this, try that and see if it fixes the issue.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
-1

From the cmake 2.8.0 documentation of AUX_SOURCE_DIRECTORY:

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

Why do you want to avoid creating a list of files? Such lists generally do not change frequently.

Mark
  • 1,035
  • 2
  • 11
  • 24
  • sorry to say but your answer is out of scope. What my question was cmake is not rebuilding the project on change of header files. – Vivek Goel Oct 28 '11 at 04:33
  • @VivekGoel - Crap, you're right. I misinterpreted it as applying to file modifications as well. Thanks for pointing out my mistake. – Mark Oct 28 '11 at 20:51