Vivek Goel
Vivek Goel

Reputation: 24140

CMAKE auto header file dependency

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 ?

Upvotes: 4

Views: 5230

Answers (3)

Edward Strange
Edward Strange

Reputation: 40849

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.

Upvotes: 1

antonakos
antonakos

Reputation: 8361

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.

Upvotes: 4

Mark
Mark

Reputation: 1053

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.

Upvotes: -1

Related Questions