I am writing some code to test that variable shouldn't be defined in a header file. However, there isn't any error. I just can't figure it out.
Please tell me why an error does not occur when I run build.bat
. The variable is defined in the header file, which is included by two source files.
The structure of the CMake project is:
--build
--include
----module1.h
--script
----build.bat
--src
----Main.cpp
----module1.cpp
----CMakeLists.txt
--CMakeLists.txt
The content of include/module1.h
is:
int a = 1;
The content of src/module1.cpp
is:
#include "module1.h"
The content of src/Main.cpp
is:
#include "module1.h"
The content of CMakeLists.txt
in the root directory is:
cmake_minimum_required(VERSION 3.0)
PROJECT(ProjectExample VERSION 0.1.0)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_FLAGS "-Wno-deprecated-declarations")
ENABLE_TESTING()
ADD_SUBDIRECTORY(src)
The content of src/CMakeLists.txt
is:
SET(INCLUDE ${CMAKE_SOURCE_DIR}/include)
FILE(GLOB SOURCE "*.cpp")
LIST(REMOVE_ITEM SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp)
ADD_COMPILE_OPTIONS("-g")
ADD_COMPILE_OPTIONS("-Wall")
ADD_LIBRARY(hello ${SOURCE})
TARGET_INCLUDE_DIRECTORIES(hello PUBLIC "${INCLUDE}")
ADD_EXECUTABLE(main ${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp)
TARGET_LINK_LIBRARIES(main hello)
The content of script/build.bat
is:
@echo off
pushd .
if not exist build (
md build
)
cd build
cmake -G"MinGW Makefiles" ..
mingw32-make.exe
popd