Issue
So recently I was working on my header files in C++ and something very strange came up. In my header files, I have my classes defined, here e.g. an audio player which uses an array of filenames to load sound samples:
#define NUM_WAVEFORMS 19
class AudioPlayer {
private:
const char _waveFileNames[NUM_WAVEFORMS][40] = {"audio/startup_seq.wav", "audio/startup_warning.wav",
"audio/start_signal.wav", "audio/warning_person.wav",
"audio/warning_bicycle.wav", "audio/warning_car.wav",
"audio/warning_motorcycle.wav", "audio/warning_bus.wav",
"audio/warning_trafficlight_red.wav", "audio/warning_trafficlight_green.wav",
"audio/warning_sidewalk.wav", "audio/warning_uphill.wav",
"audio/warning_downhill.wav", "audio/suggestion_bench.wav",
"audio/suggestion_chair.wav", "audio/suggestion_bin.wav",
"audio/to_day.wav", "audio/to_night.wav", "audio/to_park.wav"};
Mix_Chunk* _sample[NUM_WAVEFORMS];
public:
...
Now what I noticed is when I change my array of filenames, rename elements or ad new ones, this changes are not applied when compiling. When I print the elements of the array in my main .cpp-file, I get wrong indexes and elements because the array is in a previous state. When I change the length of the array by 1 and try to legit access the last element, it gives me a segmentation fault, meaning it assumes it doesn't exist. What is going on here? I have a feeling my header files don't get compiled properly.
Here's my CMakeLists.txt: (Yolo_with_Opencv is my main C++ file)
cmake_minimum_required (VERSION 3.5)
include(FindPkgConfig)
project (Yolo_with_Opencv)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -pthread")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
SET (OpenCV_DIR "/home/installation/OpenCV-3.3.1/opencv/include/opencv2")
file (GLOB source_files "${source_dir}/Yolo_with_Opencv.cpp")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
pkg_check_modules(SDL2_Mixer REQUIRED IMPORTED_TARGET SDL2_mixer)
find_package(ALSA REQUIRED)
include_directories(${ALSA_INCLUDE_DIRS})
add_executable (Yolo_with_Opencv ${source_files})
target_include_directories(Yolo_with_Opencv PRIVATE ${source_dir})
target_link_libraries(Yolo_with_Opencv ${OpenCV_LIBS})
target_link_libraries(Yolo_with_Opencv ${SDL2_LIBRARIES})
target_link_libraries(Yolo_with_Opencv PkgConfig::SDL2_Mixer)
target_link_libraries(Yolo_with_Opencv ${ALSA_LIBRARIES})
target_link_libraries(Yolo_with_Opencv i2c)
Thanks!
Solution
I think, the reason why cmake does not monitor changes of your file is that the file is not added to the target as dependency. You defined source_fles as a single cpp file. Add your header file to the source_file variable and any changes in the file will trigger recompiling.
Answered By - Stanislav Melnikov