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 ?

3
  • The dependency management is working fine for me for this example (using GCC and CMake 2.8.0 on Linux). Commented Oct 27, 2011 at 19:28
  • @antonakos is it recompiling the project on modification of main.h header file ? Commented Oct 28, 2011 at 4:34
  • @antonakos specifying minimum version as 2.8 it worked can you please put your comment as answer ? Commented Oct 28, 2011 at 7:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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.

2 Comments

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.
@VivekGoel - Crap, you're right. I misinterpreted it as applying to file modifications as well. Thanks for pointing out my mistake.

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.