Hello. I am trying to learn CMake using the Tutorial. I am at Step 2.
The contents of my main CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
${EXTRA_INCLUDES}
)
Upon building I am getting the error:
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Tutorial
[ 75%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
/home/samjnaa/sr/_repos/git/cmake/Help/guide/tutorial/Step2copy/tutorial.cxx:26:30: error: use of undeclared identifier 'mysqrt'
const double outputValue = mysqrt(inputValue);
^
1 error generated.
make[2]: *** [CMakeFiles/Tutorial.dir/build.make:63: CMakeFiles/Tutorial.dir/tutorial.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/Tutorial.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
Please advise as to what I’m doing wrong. Please ask if I need to post the contents of any other file as well. Thanks!
The files in Step3 are the expected result if you do everything from Step2.
Can you please compare your files against the ones from Step3 to see if there are any relevant differences?
Based on the compiler error message, the function prototype of mysqrt is missing.
This can be caused by not including the header with the Prototype, including another header file with the same name but without prototype or by a misspelled function name.
Can you please add a warning into the MathFunctions.h to check if it really was included:
#warning Header included
The output of you make should look like this:
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx.o
In file included from /home/u/cmake/Help/guide/tutorial/Step3/MathFunctions/mysqrt.cxx:3:
/home/u/cmake/Help/guide/tutorial/Step3/MathFunctions/MathFunctions.h:1:2: warning: #warning Header included [-Wcpp]
#warning Header included
^~~~~~~
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Tutorial
[ 75%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
In file included from /home/u/cmake/Help/guide/tutorial/Step3/tutorial.cxx:10:
/home/u/cmake/Help/guide/tutorial/Step3/MathFunctions/MathFunctions.h:1:2: warning: #warning Header included [-Wcpp]
#warning Header included
^~~~~~~
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
So you can see, that the header was really included by both .cxx files.
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
Which is about the same problem, but for CMake not C: Do not use a symbol before its definition.
So probably there should be added a hint or the complete code of the inclusion to the tutorial.