C++ - cmake cannot find boost libraries - c++

For a project, I have to use cmake in combination with boost to generate a C++ build (avro). I have installed the following programs:
cmake v3.4.3
boost v1.55.0
But when executing the command with the following statements in the .bat file (this batch file got provided by a 3th party provider),
set BOOST_ROOT=D:\software\boost_1_55_0\
set BOOST_INCLUDEDIR=D:\software\boost_1_55_0\boost
set BOOST_LIBRARYDIR=D:\software\boost_1_55_0\lib32-msvc-12.0
set PATH=%MSVS_HOME%\VC;%BOOST_LIBRARYDIR%;%BOOST_ROOT%;%BOOST_INCLUDEDIR%;%PATH%
call vcvarsall.bat
cmake .. -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug
(remark: MSVS_HOME is the directory where Visual Studio got installed to and the other paths are set by my own.)
cmake throws up with errors, as shown here below
CMake Error at D:/software/cmake_3.4.3/share/cmake-3.4/Modules/FindBoost.cmake:1247 (message):
Unable to find the requested Boost libraries.
Boost version: 1.55.0
Boost include path: D:/software/boost_1_55_0
Could not find the following Boost libraries:
boost_filesystem
boost_system
boost_program_options
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of Boost.
Call Stack (most recent call first):
CMakeLists.txt:54 (find_package)
-- Configuring incomplete, errors occurred!
See also "D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeOutput.log".
It however surprises me because in the boost directory, you can find the required libraries. Here below are PS commands that I have used to find the required Boost libraries.
PS D:\software\boost_1_55_0\lib32-msvc-12.0> Get-ChildItem boost_file* -name
boost_filesystem-vc120-1_55.dll
boost_filesystem-vc120-1_55.lib
boost_filesystem-vc120-gd-1_55.dll
boost_filesystem-vc120-gd-1_55.lib
boost_filesystem-vc120-mt-1_55.dll
boost_filesystem-vc120-mt-1_55.lib
boost_filesystem-vc120-mt-gd-1_55.dll
boost_filesystem-vc120-mt-gd-1_55.lib
PS D:\software\boost_1_55_0\lib32-msvc-12.0> Get-ChildItem boost_system* -name
boost_system-vc120-1_55.dll
boost_system-vc120-1_55.lib
boost_system-vc120-gd-1_55.dll
boost_system-vc120-gd-1_55.lib
boost_system-vc120-mt-1_55.dll
boost_system-vc120-mt-1_55.lib
boost_system-vc120-mt-gd-1_55.dll
boost_system-vc120-mt-gd-1_55.lib
PS D:\software\boost_1_55_0\lib32-msvc-12.0> Get-ChildItem boost_program_opt* -name
boost_program_options-vc120-1_55.dll
boost_program_options-vc120-1_55.lib
boost_program_options-vc120-gd-1_55.dll
boost_program_options-vc120-gd-1_55.lib
boost_program_options-vc120-mt-1_55.dll
boost_program_options-vc120-mt-1_55.lib
boost_program_options-vc120-mt-gd-1_55.dll
As you can see, the required file is available in the right location.
Question :
I am not familiar with cmake. Could the problem be at the cmake list (here below) or is the problem somewhere else ? I already have looked into the following questions:
c++ - cmake cannot find boost libraries
The answer goes about a configuration problem. But since I have barely experience with cmake, I am not sure if the configuration is correct. I didn't have configured it. These files got provided to me by a 3th party provider...
C++ - CMAKE cannot find boost
There is no answer, but in the comment, it mentions about the variables. I have set it correctly here (I think ...)
Cmake doesn't find Boost
The answer tells to add those two lines before the FIND_PACKAGE(Boost) but my cmakelists.txt (here below) doesn't contain it.
C++ - CMake is not able to find boost libraries
The answers were vague, I don't know which one would be useful. There is an answer that mentions a snippet which can be added to the cmakelists.txt. I'm not sure if I should do that. (As said, the files got provided by a 3th party provider)
I have also checked the following answers outside StackOverflow:
this, which tells about the environment variables, something what I already have
this, it mentions about a version of libdev boost. I do think that I have the latest version. (see above, I have Boost 1.55)
this, that mentions to remove the cmake file. However, it works for orocos, something that I don't use. I don't think that this wouldn't solve my problem either. But I tried it anyways, and no, didn't helped.
So, what can I do to solve this problem ? Did I have overlooked on something ? Missed something important ?
There are two snippets here below, the cmake list and the cmake log, generated after the aboved failed execution. I hope that these are useful.
CMakeLists.txt:
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
cmake_minimum_required (VERSION 2.6)
set (CMAKE_LEGACY_CYGWIN_WIN32 0)
if (NOT DEFINED CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS)
set (CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
endif()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt)
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" AVRO_VERSION)
else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt)
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/../../share/VERSION.txt"
AVRO_VERSION)
endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt)
set (AVRO_VERSION_MAJOR ${AVRO_VERSION})
set (AVRO_VERSION_MINOR "0")
project (Avro-cpp)
if (WIN32 AND NOT CYGWIN AND NOT MSYS)
add_definitions (/EHa)
add_definitions (
-DBOOST_REGEX_DYN_LINK
-DBOOST_FILESYSTEM_DYN_LINK
-DBOOST_SYSTEM_DYN_LINK
-DBOOST_PROGRAM_OPTIONS_DYN_LINK
-DBOOST_ALL_NO_LIB)
endif()
if (CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-Wall")
endif ()
find_package (Boost 1.38 REQUIRED
COMPONENTS filesystem system program_options)
add_definitions (${Boost_LIB_DIAGNOSTIC_DEFINITIONS})
include_directories (api ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIRS})
set (AVRO_SOURCE_FILES
impl/Compiler.cc impl/Node.cc
impl/NodeImpl.cc impl/ResolverSchema.cc impl/Schema.cc
impl/Types.cc impl/ValidSchema.cc impl/Zigzag.cc
impl/BinaryEncoder.cc impl/BinaryDecoder.cc
impl/Stream.cc impl/FileStream.cc
impl/Generic.cc
impl/DataFile.cc
impl/parsing/Symbol.cc
impl/parsing/ValidatingCodec.cc
impl/parsing/JsonCodec.cc
impl/parsing/ResolvingDecoder.cc
impl/json/JsonIO.cc
impl/json/JsonDom.cc
impl/Resolver.cc impl/Validator.cc
)
add_library (avrocpp SHARED ${AVRO_SOURCE_FILES})
set_property (TARGET avrocpp
APPEND PROPERTY COMPILE_DEFINITIONS AVRO_DYN_LINK)
add_library (avrocpp_s STATIC ${AVRO_SOURCE_FILES})
set_property (TARGET avrocpp avrocpp_s
APPEND PROPERTY COMPILE_DEFINITIONS AVRO_SOURCE)
set_target_properties (avrocpp PROPERTIES
VERSION ${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR})
set_target_properties (avrocpp_s PROPERTIES
VERSION ${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR})
target_link_libraries (avrocpp ${Boost_LIBRARIES})
add_executable (precompile test/precompile.cc)
target_link_libraries (precompile avrocpp_s ${Boost_LIBRARIES})
macro (gencpp file ns)
add_custom_command (OUTPUT ${ns}.hh
COMMAND precompile ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file}
${file}
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen-cppcode.py
-n ${ns} -i ${file} -o ${ns}.hh
DEPENDS precompile ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file})
add_custom_target(${ns} DEPENDS ${ns}.hh)
endmacro (gencpp)
if (CYGWIN OR NOT WIN32)
gencpp (bigrecord testgen)
gencpp (bigrecord2 testgen2)
endif ()
macro (gen file ns)
add_custom_command (OUTPUT ${file}.hh
COMMAND avrogencpp
-p -
-i ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file}
-o ${file}.hh -n ${ns} -U
DEPENDS avrogencpp ${CMAKE_CURRENT_SOURCE_DIR}/jsonschemas/${file})
add_custom_target (${file}_hh DEPENDS ${file}.hh)
endmacro (gen)
gen (bigrecord testgen)
gen (bigrecord2 testgen2)
gen (tweet testgen3)
gen (union_array_union uau)
gen (union_map_union umu)
gen (union_conflict uc)
gen (recursive rec)
gen (reuse ru)
gen (circulardep cd)
add_executable (avrogencpp impl/avrogencpp.cc)
target_link_libraries (avrogencpp avrocpp_s ${Boost_LIBRARIES})
enable_testing()
macro (unittest name)
add_executable (${name} test/${name}.cc)
target_link_libraries (${name} avrocpp ${Boost_LIBRARIES})
add_test (NAME ${name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${name})
endmacro (unittest)
unittest (buffertest)
unittest (unittest)
unittest (SchemaTests)
unittest (CodecTests)
unittest (StreamTests)
unittest (SpecificTests)
unittest (DataFileTests)
unittest (JsonTests)
unittest (AvrogencppTests)
if (CYGWIN OR NOT WIN32)
unittest (testgentest)
add_dependencies (testgentest testgen testgen2)
endif()
add_dependencies (AvrogencppTests bigrecord_hh bigrecord2_hh tweet_hh
union_array_union_hh union_map_union_hh union_conflict_hh
recursive_hh reuse_hh circulardep_hh)
include (InstallRequiredSystemLibraries)
set (CPACK_PACKAGE_FILE_NAME "avrocpp-${AVRO_VERSION_MAJOR}")
include (CPack)
install (TARGETS avrocpp avrocpp_s
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION lib)
install (TARGETS avrogencpp RUNTIME DESTINATION bin)
install (DIRECTORY api/ DESTINATION include/avro
FILES_MATCHING PATTERN *.hh)
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif (NOT CMAKE_BUILD_TYPE)
and the cmake log:
The system is: Windows - 10.0.10586 - AMD64
Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: C:/Program Files (x86)/Visual Studio 14.0/VC/bin/cl.exe
Build flags:
Id flags:
The output was:
0
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
CMakeCCompilerId.c
Microsoft (R) Incremental Linker Version 14.00.23506.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:CMakeCCompilerId.exe
CMakeCCompilerId.obj
Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CMakeCCompilerId.exe"
Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CMakeCCompilerId.obj"
The C compiler identification is MSVC, found in "D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/3.4.3/CompilerIdC/CMakeCCompilerId.exe"
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: C:/Program Files (x86)/Visual Studio 14.0/VC/bin/cl.exe
Build flags:
Id flags:
The output was:
0
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
CMakeCXXCompilerId.cpp
Microsoft (R) Incremental Linker Version 14.00.23506.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:CMakeCXXCompilerId.exe
CMakeCXXCompilerId.obj
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CMakeCXXCompilerId.exe"
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CMakeCXXCompilerId.obj"
The CXX compiler identification is MSVC, found in "D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/3.4.3/CompilerIdCXX/CMakeCXXCompilerId.exe"
Determining if the C compiler works passed with the following output:
Change Dir: D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_279c3\fast"
"C:\Program Files (x86)\Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_279c3.dir\build.make /nologo -L CMakeFiles\cmTC_279c3.dir\build
Building C object CMakeFiles/cmTC_279c3.dir/testCCompiler.c.obj
C:\PROGRA~2\VISUAL~1.0\VC\bin\cl.exe #C:\Users\geire\AppData\Local\Temp\nm8C78.tmp
testCCompiler.c
Linking C executable cmTC_279c3.exe
D:\software\cmake_3.4.3\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_279c3.dir --manifests -- C:\PROGRA~2\VISUAL~1.0\VC\bin\link.exe /nologo #CMakeFiles\cmTC_279c3.dir\objects1.rsp #C:\Users\geire\AppData\Local\Temp\nm8DF0.tmp
Detecting C compiler ABI info compiled with the following output:
Change Dir: D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_f6e85\fast"
"C:\Program Files (x86)\Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_f6e85.dir\build.make /nologo -L CMakeFiles\cmTC_f6e85.dir\build
Building C object CMakeFiles/cmTC_f6e85.dir/CMakeCCompilerABI.c.obj
C:\PROGRA~2\VISUAL~1.0\VC\bin\cl.exe #C:\Users\geire\AppData\Local\Temp\nm908F.tmp
CMakeCCompilerABI.c
Linking C executable cmTC_f6e85.exe
D:\software\cmake_3.4.3\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_f6e85.dir --manifests -- C:\PROGRA~2\VISUAL~1.0\VC\bin\link.exe /nologo #CMakeFiles\cmTC_f6e85.dir\objects1.rsp #C:\Users\geire\AppData\Local\Temp\nm90CF.tmp
Determining if the CXX compiler works passed with the following output:
Change Dir: D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_38c4b\fast"
"C:\Program Files (x86)\Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_38c4b.dir\build.make /nologo -L CMakeFiles\cmTC_38c4b.dir\build
Building CXX object CMakeFiles/cmTC_38c4b.dir/testCXXCompiler.cxx.obj
C:\PROGRA~2\VISUAL~1.0\VC\bin\cl.exe #C:\Users\geire\AppData\Local\Temp\nm92E1.tmp
testCXXCompiler.cxx
Linking CXX executable cmTC_38c4b.exe
D:\software\cmake_3.4.3\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_38c4b.dir --manifests -- C:\PROGRA~2\VISUAL~1.0\VC\bin\link.exe /nologo #CMakeFiles\cmTC_38c4b.dir\objects1.rsp #C:\Users\geire\AppData\Local\Temp\nm9330.tmp
Detecting CXX compiler ABI info compiled with the following output:
Change Dir: D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_c9164\fast"
"C:\Program Files (x86)\Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_c9164.dir\build.make /nologo -L CMakeFiles\cmTC_c9164.dir\build
Building CXX object CMakeFiles/cmTC_c9164.dir/CMakeCXXCompilerABI.cpp.obj
C:\PROGRA~2\VISUAL~1.0\VC\bin\cl.exe #C:\Users\geire\AppData\Local\Temp\nm95DE.tmp
CMakeCXXCompilerABI.cpp
Linking CXX executable cmTC_c9164.exe
D:\software\cmake_3.4.3\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_c9164.dir --manifests -- C:\PROGRA~2\VISUAL~1.0\VC\bin\link.exe /nologo #CMakeFiles\cmTC_c9164.dir\objects1.rsp #C:\Users\geire\AppData\Local\Temp\nm962E.tmp
Detecting CXX [] compiler features compiled with the following output:
Change Dir: D:/kaa/kaaBuild/avro-src-1.7.5/lang/c++/build.win/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_2af8b\fast"
"C:\Program Files (x86)\Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_2af8b.dir\build.make /nologo -L CMakeFiles\cmTC_2af8b.dir\build
Building CXX object CMakeFiles/cmTC_2af8b.dir/feature_tests.cxx.obj
C:\PROGRA~2\VISUAL~1.0\VC\bin\cl.exe #C:\Users\geire\AppData\Local\Temp\nm98CC.tmp
feature_tests.cxx
Linking CXX executable cmTC_2af8b.exe
D:\software\cmake_3.4.3\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_2af8b.dir --manifests -- C:\PROGRA~2\VISUAL~1.0\VC\bin\link.exe /nologo #CMakeFiles\cmTC_2af8b.dir\objects1.rsp #C:\Users\geire\AppData\Local\Temp\nm992B.tmp
Feature record: CXX_FEATURE:1cxx_alias_templates
Feature record: CXX_FEATURE:1cxx_alignas
Feature record: CXX_FEATURE:1cxx_alignof
Feature record: CXX_FEATURE:1cxx_attributes
Feature record: CXX_FEATURE:1cxx_attribute_deprecated
Feature record: CXX_FEATURE:1cxx_auto_type
Feature record: CXX_FEATURE:1cxx_binary_literals
Feature record: CXX_FEATURE:1cxx_constexpr
Feature record: CXX_FEATURE:1cxx_contextual_conversions
Feature record: CXX_FEATURE:1cxx_decltype
Feature record: CXX_FEATURE:1cxx_decltype_auto
Feature record: CXX_FEATURE:1cxx_default_function_template_args
Feature record: CXX_FEATURE:1cxx_defaulted_functions
Feature record: CXX_FEATURE:1cxx_defaulted_move_initializers
Feature record: CXX_FEATURE:1cxx_delegating_constructors
Feature record: CXX_FEATURE:1cxx_deleted_functions
Feature record: CXX_FEATURE:1cxx_digit_separators
Feature record: CXX_FEATURE:1cxx_enum_forward_declarations
Feature record: CXX_FEATURE:1cxx_explicit_conversions
Feature record: CXX_FEATURE:1cxx_extended_friend_declarations
Feature record: CXX_FEATURE:1cxx_extern_templates
Feature record: CXX_FEATURE:1cxx_final
Feature record: CXX_FEATURE:1cxx_func_identifier
Feature record: CXX_FEATURE:1cxx_generalized_initializers
Feature record: CXX_FEATURE:1cxx_generic_lambdas
Feature record: CXX_FEATURE:1cxx_inheriting_constructors
Feature record: CXX_FEATURE:1cxx_inline_namespaces
Feature record: CXX_FEATURE:1cxx_lambdas
Feature record: CXX_FEATURE:1cxx_lambda_init_captures
Feature record: CXX_FEATURE:1cxx_local_type_template_args
Feature record: CXX_FEATURE:1cxx_long_long_type
Feature record: CXX_FEATURE:1cxx_noexcept
Feature record: CXX_FEATURE:1cxx_nonstatic_member_init
Feature record: CXX_FEATURE:1cxx_nullptr
Feature record: CXX_FEATURE:1cxx_override
Feature record: CXX_FEATURE:1cxx_range_for
Feature record: CXX_FEATURE:1cxx_raw_string_literals
Feature record: CXX_FEATURE:1cxx_reference_qualified_functions
Feature record: CXX_FEATURE:1cxx_return_type_deduction
Feature record: CXX_FEATURE:1cxx_right_angle_brackets
Feature record: CXX_FEATURE:1cxx_rvalue_references
Feature record: CXX_FEATURE:1cxx_sizeof_member
Feature record: CXX_FEATURE:1cxx_static_assert
Feature record: CXX_FEATURE:1cxx_strong_enums
Feature record: CXX_FEATURE:1cxx_template_template_parameters
Feature record: CXX_FEATURE:1cxx_thread_local
Feature record: CXX_FEATURE:1cxx_trailing_return_types
Feature record: CXX_FEATURE:1cxx_unicode_literals
Feature record: CXX_FEATURE:1cxx_uniform_initialization
Feature record: CXX_FEATURE:1cxx_unrestricted_unions
Feature record: CXX_FEATURE:1cxx_user_literals
Feature record: CXX_FEATURE:1cxx_variadic_macros
Feature record: CXX_FEATURE:1cxx_variadic_templates

Related

How to change makefile generator for CMake?

Asked this question:
CMake cannot indentify path to compiler mentioned by set(CMAKE_C_COMPILER "path") rule
received an incorrect rules order response(it was obviously correct).
Aplied changes and tried again:
cmake .
output:
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: nmake /nologo cmTC_fc6d3\fast &&
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe - broken
But I need not nmake but mingw make file ,found this topic:
CMake Error : execution of make failed on Windows
The 100% same problem I have .Tried provided solution:
You need to specify a generator in CMake command line like -G "MinGW
Makefiles" and - depending on how many compilers you have installed -
also the full paths to the compilers.
cmake -G "MinGW Makefiles"
output:
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
CMake Error: Error: generator : MinGW Makefiles
Does not match the generator used previously: NMake Makefiles
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
and next cmake . output gives (after deleting CMakeCache.txt file and CMakeFiles directory as mentioned above):
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
CMake Error: CMake was unable to find a build program corresponding to "NMake Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:44 (try_compile):
Failed to configure test project build system.
Call Stack (most recent call first):
CMakeLists.txt:9 (project)
-- Configuring incomplete, errors occurred!
See also "CMakeFiles/CMakeOutput.log".
CMake Error: Generator: execution of make failed. Make command was: cmTC_4b6e5/fast &&
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe - broken
The 3 things i cannot understand:
How to ... specify ... the full paths to the compilers or needed source or binary directory.
Why compilers are connected to make.exe in any way.
Why CMAKE_MAKE_PROGRAM is not set if there is proper rule.
Changing rules order :
set( CMAKE_MAKE_PROGRAM FORCE ) set(CMAKE_C_COMPILER ) set(CMAKE_CXX_COMPILER ) and
set(CMAKE_C_COMPILER ) set(CMAKE_CXX_COMPILER ) set( CMAKE_MAKE_PROGRAM FORCE ) has the same result after call.
If I remove set( CMAKE_MAKE_PROGRAM ) rule configuring with CMake GUI is possible but with cmd manual cmake . call is not .Same output as provided above.
Inside CMakeCache.txt generated by CMake GUI call's are lines :
//Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=C:/Strawberry/c/bin/mingw32-make.exe
so for it was possible to find mingw32-make.exe and path provided for manual call is also right.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.1)
#set(CMAKE_MAKE_PROGRAM C:/Strawberry/c/bin/make.exe FORCE )
set( CMAKE_MAKE_PROGRAM C:/Strawberry/c/bin/mingw32-make.exe FORCE )
set(CMAKE_C_COMPILER "C:/Strawberry/c/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/Strawberry/c/bin/g++.exe")
project("Client")
enable_testing()
set(CMAKE_CXX_FLAGS "-std=c++17 " )
add_executable(Client
main.cpp
client.cpp
client.h
logmsg.cpp
includes.h
)
target_link_libraries(Client wsock32 ws2_32)
add_test(NAME test COMMAND Client )

Configure CMake in windows to use clang from command line to get modern OpenMP support

I have a small test project that uses OpenMP for parallelization. My target is to compile it so it produces .dll and .lib for libraries (because my real project links to external libraries distributed using these types), with support for OpenMP 4.5 or newer and do it from the command line so it can be done on a docker for testing and checking (the docker part is out of the scope of this question, it is just for reference on why I need it to work from the command line). I can compile this project with different compilers with which I am not happy:
MSVC: mkdir build-msvc, then cmake .. (from the newly created folder) and finally cmake --build . --config Release. This compiles fine but only supports OpenMP 2.0, therefore is not a good option for my real project.
Intel Parallel Studio: mkdir build-intel, then cmake .. -T "Intel C++ Compiler 19.0" (from the newly created folder) and finally cmake --build . --config Release. This supports OpenMP 5.0 but its licenses are pretty expensive for me.
MinGW x64 g++: mkdir build-g++, then cmake .. -G "MinGW Makefiles" (from the newly created folder) and finally cmake --build .. It supports OpenMP 4.5 but this compiler is not compatible wiht .lib (as far as I know) which I already mentioned is necessary for me.
I have tried without success to use clang:
CLANG from MSVC: mkdir build-clang-msvc, then cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl .. (from the newly created folder), but it fails with the following error:
-- The CXX compiler identification is Clang 8.0.1 with MSVC-like command-line
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.15/Modules/CMakeTestCXXCompiler.cmake:53 (message):
The C++ compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeTmp
Run Build Command(s):C:/PROGRA~2/MICROS~1/2019/COMMUN~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe cmTC_bd131 && [1/2] Building CXX object CMakeFiles\cmTC_bd131.dir\testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_bd131.exe
FAILED: cmTC_bd131.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_bd131.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_bd131.dir\testCXXCompiler.cxx.obj /out:cmTC_bd131.exe /implib:cmTC_bd131.lib /pdb:cmTC_bd131.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
RC Pass 1: command "rc /fo CMakeFiles\cmTC_bd131.dir/manifest.res CMakeFiles\cmTC_bd131.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specified
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeOutput.log".
See also "C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeError.log".
Clang and Ninja apart from MSVC: mkdir build-clang-ninja, then cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl .. (from the newly created folder and changing the PATH environmental variable so the non-msvc are found first), but it fails with the following error:
-- The CXX compiler identification is Clang 9.0.0 with MSVC-like command-line
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.15/Modules/CMakeTestCXXCompiler.cmake:53 (message):
The C++ compiler
"C:/Program Files/LLVM/bin/clang-cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Ninja/ninja.exe cmTC_50b73 && [1/2] Building CXX object CMakeFiles\cmTC_50b73.dir\testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_50b73.exe
FAILED: cmTC_50b73.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_50b73.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- CMAKE_LINKER-NOTFOUND /nologo CMakeFiles\cmTC_50b73.dir\testCXXCompiler.cxx.obj /out:cmTC_50b73.exe /implib:cmTC_50b73.lib /pdb:cmTC_50b73.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
RC Pass 1: command "rc /fo CMakeFiles\cmTC_50b73.dir/manifest.res CMakeFiles\cmTC_50b73.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specified
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeOutput.log".
See also "C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeError.log".
Any ideas on how to proceed with the clang? I think it is my best option for what I want to achieve (compile my test program with support for OpenMP 4.5 or newer and producing .lib and .dll).
Related posts/webpages that I have checked but have been non-useful to solve this:
Building c++ project on Windows with CMake, Clang and Ninja
Building with CMake, Ninja and Clang on Windows
Ways to Compile with Clang on Windows, specifically setting the linker: -DCMAKE_LINKER="C:/Program Files/LLVM/bin/lld-link.exe"
CMake building for Windows (clang-cl) using Ninja Generator
Clang-cl user manual
I was able to duplicate this error for the Clang+MSVC case. Because we are attempting to use the Visual-Studio-tailored compiler from outside Visual Studio (i.e. the command line), it is necessary to initialize the VS build environment in our command line before using the compiler. The VCVarsXX.bat files accomplish this; they are part of the VS Command Prompt tools. So by picking your architecture (x86, x64, etc) and running the script, this should allow CMake to build the simple test program with clang-cl and proceed. Here is where it is located for VS 2019:
>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
>cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
-- The CXX compiler identification is Clang 8.0.1
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
...
To expand on the OpenMP issue, I personally found using find_package(OpenMP REQUIRED) to be pretty fruitless. As you mentioned, I also received this CMake error:
Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
There are a few suggestions on this site (for example here and here) claiming that populating all of the variables manually allows FindOpenMP.cmake to successfully find the libraries. I tried this with your example CMake file, with some success:
cmake_minimum_required (VERSION 2.8)
project(test_openmp LANGUAGES CXX)
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(OpenMP_CXX_LIB_NAMES "libomp" "libiomp5")
set(OpenMP_libomp_LIBRARY libomp)
set(OpenMP_libiomp5_LIBRARY libiomp5)
#OPENMP
find_package(OpenMP REQUIRED)
if(OPENMP_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
# Library
add_library(example_lib SHARED example_lib.h example_lib.cpp)
target_link_libraries(example_lib PUBLIC ${OpenMP_CXX_LIBRARIES})
target_compile_definitions(example_lib PRIVATE EXEMPLE_LIB_EXPORT)
# Executable
add_executable(test_openmp test_openmp.cpp)
target_link_libraries(test_openmp example_lib)
This succeeded in "finding" the libraries on the second CMake configuration attempt (the first always yielded the same Could NOT find OpenMP_CXX error as before):
>cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done
However, this failed to build the executable, as none of the CMake OpenMP variables (particularly OpenMP_CXX_LIBRARIES) actually point to the library location. The shortcomings of CMake's FindOpenMP.cmake have been raised on the CMake issue site here, and there appear to be relevant limitations on the LLVM/Clang end as well.
Regardless, the cleanest way I was able to get the example working was to ditch find_package() altogether. The following allowed me to successfully generate the buildsystem, and compile and run the executable:
cmake_minimum_required (VERSION 2.8)
project(test_openmp LANGUAGES CXX)
set(OpenMP_LIBRARY_DIR "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/lib")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# Tell CMake where to find the OpenMP libraries.
link_directories(${OpenMP_LIBRARY_DIR})
# Library
add_library(example_lib SHARED example_lib.h example_lib.cpp)
# Link in the OpenMP libraries.
target_link_libraries(example_lib PUBLIC libomp libiomp5md)
target_compile_definitions(example_lib PRIVATE EXEMPLE_LIB_EXPORT)
# Executable
add_executable(test_openmp test_openmp.cpp)
target_link_libraries(test_openmp example_lib)

Torch CMake Error at CMakeLists.txt:10 (find_package) in C++

I am trying to create a Eclipse C++ project by CMake which calls torch/torch.h . I run cmake -G "Eclipse CDT4 - Unix Makefiles" ./ to create a Eclipse project, but I get this error:
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindTorch.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Torch", but
CMake did not find one.
Could not find a package configuration file provided by "Torch" with any of
the following names:
TorchConfig.cmake
torch-config.cmake
Add the installation prefix of "Torch" to CMAKE_PREFIX_PATH or set
"Torch_DIR" to a directory containing one of the above files. If "Torch"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
In which CMakeLists.txt is located in the current directory that has:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)
find_package(Torch REQUIRED)
add_executable(test test.cpp)
target_link_libraries(test "${TORCH_LIBRARIES}")
set_property(TARGET test PROPERTY CXX_STANDARD 11)
Apparently, it cannot find TorchConfig.cmake and torch-config.cmake files; although, I have TorchConfig.cmake in /home/afshin/libtorch/share/cmake/Torch. I added the corresponding path into the CMakeLists.txt file by testing each of:
set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake/Torch;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch;${CMAKE_MODULE_PATH}")
set(Torch_DIR "/home/afshin/libtorch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake/Torch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake;${Torch_DIR}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch;${DCMAKE_PREFIX_PATH}")
But, it did not help and I still get same error.
I appreciate any help or comments.
I also tried the cmake-gui and I get same error:
Thanks,
Afshin
I was able to solve the problem by editing the CMakeLists.txt as:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test_cmake)
set(CMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
add_executable(test_cmake ./src/test_cmake.cpp)
target_link_libraries(test_cmake "${TORCH_LIBRARIES}")
set_property(TARGET test_cmake PROPERTY CXX_STANDARD 11)
Alternatively, using the cmake-gui also I was able to solve the problem by the following setting:
by hitting configure, and then generate.
Finally, I imported this project into Eclipse by selecting Makefile Project With Existing Code. This code is compiled and built successfully.
The following modified CMakeLists.txt file works without the apparently missing TorchConfig.cmake (also missing in the vcpkg installation here). I recommend Microsoft's vcpkg for cross-platform packages (c++ libraries) management (usage here). But the code is vcpkg independent: one can set TORCH_BASE_PATH (or Torch_INCLUDE_DIR and Torch_LIBRARIES) to the proper paths.
#[[
tested with:
- CMake 3.13
- Visual Studio Community Edition 15.9.4
(CMake generator: "Visual Studio 15 2017 Win64")
- torch-th library installed with vcpkg
generic: vcpkg install torch-th
for macOS: vcpkg install torch-th:x64-osx-dynamic
x64-osx-dynamic triplet must be created: x64-osx + "set(VCPKG_LIBRARY_LINKAGE dynamic)"
for Windows: vcpkg install torch-th:x64-windows
- easy torch sample: https://apaszke.github.io/torch-internals.html
]]
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)
# cannot work without a "package configuration file" (TorchConfig.cmake)
# so we replace it with find_path and find_library
#find_package(Torch REQUIRED)
#[[
the environement variable VCPKG_ROOT used here, contains the path to vcpkg installation folder
replace the two paths with your paths to TH installation
usage: #include "TH/TH.h"
]]
set(TORCH_BASE_PATH "$ENV{VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}")
message(STATUS TORCH_BASE_PATH=${TORCH_BASE_PATH})
set(Torch_INCLUDE_DIR "${TORCH_BASE_PATH}/include")
set(Torch_LIBRARIES "${TORCH_BASE_PATH}/lib")
# target_link_libraries is to be preferred
#link_directories(${Torch_LIBRARIES})
find_library(LIBRARY_TORCH TH HINTS ${Torch_LIBRARIES})
#[[
even simpler
if you use the vcpkg toolchain file "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
find_path(Torch_INCLUDE_DIR TH/TH.h)
find_library(LIBRARY_TORCH TH)
]]
add_executable(test test.cpp)
target_include_directories(test PRIVATE ${Torch_INCLUDE_DIR})
target_link_libraries(test ${LIBRARY_TORCH})
set(CMAKE_CXX_STANDARD 11)
#set_property(TARGET test PROPERTY CXX_STANDARD 11)

Compiling an OpenCV project using CMake on Cygwin, with OpenCV installed for Windows

Situation:
I have a Windows 7 machine with OpenCV for Windows installed on it.
My OpenCV C++ projects work fine with Visual Studio 2010.
I want to run my existing OpenCV C++ projects to run on Raspberry Pi and on other Linux machines.
Trouble
As a first step, I am trying to compile my .cpp & .h files using GCC on Cygwin on my Windows Machine. For this purpose I am trying to use CMake package for Cygwin, but CMake gives error that it cannot find OpenCV package. (error is listed below).
Question:
Do i need to install OpenCV package for Cygwin separately for CMake to work ?
Is there a way to using my existing OpenCV for Win to work with CMake ?
What environment variables need to be set ?
(Currently only a variable OPENCV_DIR is set to C:/opencv/build)
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
PROJECT (testProj)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
src/imgProcess.cpp
)
set( NAME_HEADERS
include/imgProcess.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( testProj ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( test ${OpenCV_LIBS} )
cmake error
$ cmake .
-- The C compiler identification is GNU 4.9.3
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++.exe
-- Check for working CXX compiler: /usr/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:3 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Work/Project/cmaketest/CMakeFiles/CMakeOutput.log".
The variable should be called OpenCV_DIR, it must have the same capitalization as in find_package(OpenCV REQUIRED).
As #berak, stated in the comment, I installed OpenCV Package for Cygwin using Cygwin Ports. (Without any src or requiring any build, simple install only).
Had to tweak my CMakeLists.txt a little but it compiled well.
##### CMakeLists.txt ########
cmake_minimum_required(VERSION 2.8.4)
PROJECT (test)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
src/mytest.cpp
)
set( NAME_HEADERS
include/mytest.hpp
)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( test ${NAME_SRC} ${NAME_HEADERS} )
include_directories(/usr/include /usr/include/opencv2 /usr/include/opencv ${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(/usr/lib ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(test opencv_core opencv_highgui opencv_imgproc)
(Now, I have totally different trouble of runtime error : Gtk-WARNING **: cannot open display, but its another story of not having XServer on Cygwin)
First take the following steps to build opencv using visual studio and cmake gui.
Clone into the opencv source code repo
$ git clone https://github.com/Itseez/opencv.git
Navigate inside the folder and checkout to the specified version
$ git checkout 3.0.0
Create a build directory and build opencv in there using CMake and visual studio. Note that by default opencv will build dynamic libraries unless you enable the option of building opencv static libraries.
Once done you should handle the environment variables. Create a variable named OpenCV_DIR and point it to the build directory of opencv. Then update your PATH variable to have new opencv dll files added. Usually they can be found in the directory opencv\build\bin\Release assuming you have built opencv in Release mode
Then set up your project CMakeList.txt as follows
find_package(OpenCV REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(test
${OpenCV_LIBS}
)
The command find_package(OpenCV REQUIRED) finds the opencv configuration files in the directory "opencv/build", aka the directory in which you have built opencv (That's why you should set the value of the variable ${OpenCV_DIR} to this directory so the command find_package can find the required files in there).
The two variables {OpenCV_INCLUDE_DIRS} and ${OpenCV_LIBS} have already been set in those configuration files. So you should be all set.

How to use the tool include-what-you-use together with CMake to detect unused headers?

The tool include-what-you-use can be used to detect unneeded headers. I am using CMake for my C++ software project. How can I instruct CMake to run include-what-you-use automatically on the source files of my software project?
CMake 3.3 introduced the new target property CXX_INCLUDE_WHAT_YOU_USE that can be set to the path of the program include-what-you-use. For instance this CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
add_executable(hello main.cc)
find_program(iwyu_path NAMES include-what-you-use iwyu REQUIRED)
# If using CGAL<3.18, you remove REQUIRED and use
# if(NOT iwyu_path)
# message(FATAL_ERROR "Could not find the program include-what-you-use")
# endif()
set_property(TARGET hello PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path})
is able to build the file main.cc
#include <iostream>
#include <vector>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
and at the same time have include-what-you-use give out a warning that
the included header vector is not needed.
user#ubuntu:/tmp$ ls ~/hello
CMakeLists.txt main.cc
user#ubuntu:/tmp$ mkdir /tmp/build
user#ubuntu:/tmp$ cd /tmp/build
user#ubuntu:/tmp/build$ ~/cmake-3.3.0-rc2-Linux-x86_64/bin/cmake ~/hello
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/build
user#ubuntu:/tmp/build$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o
Warning: include-what-you-use reported diagnostics:
/home/user/hello/main.cc should add these lines:
/home/user/hello/main.cc should remove these lines:
- #include <vector> // lines 2-2
The full include-list for /home/user/hello/main.cc:
#include <iostream> // for operator<<, basic_ostream, cout, endl, ostream
---
[100%] Linking CXX executable hello
[100%] Built target hello
user#ubuntu:/tmp/build$ ./hello
Hello World!
user#ubuntu:/tmp/build$
If you want to pass custom options to include-what-you-use, like for instance --mapping_file you can do it via
set(iwyu_path_and_options
${iwyu_path}
-Xiwyu
--mapping_file=${my_mapping})
set_property(TARGET hello
PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path_and_options})
If you don't have access to CMake 3.3, include-what-you-use comes with a python tool called iwyu_tool.py which can do what you want.
It works by parsing a JSON compilation database, which is easily produced with CMake (see below).
Running the tool manually
Assuming you already have a CMake build dir for your project, you first need to tell CMake to produce the compilation database:
$ cd build
$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
This generates a file, compile_commands.json containing compiler invocations for every object file in your project. You don't need to rebuild the project.
You can now run include-what-you-use on your project by running the python tool on your build directory:
$ python /path/to/iwyu_tool.py -p .
Adding a custom target to your cmake project
The following snippet can be used to add an iwyu target to a cmake project.
# Generate clang compilation database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(PythonInterp)
find_program(iwyu_tool_path NAMES iwyu_tool.py)
if (iwyu_tool_path AND PYTHONINTERP_FOUND)
add_custom_target(iwyu
ALL # Remove ALL if you don't iwyu to be run by default.
COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_path}" -p "${CMAKE_BINARY_DIR}"
COMMENT "Running include-what-you-use tool"
VERBATIM
)
endif()
Notes
The include-what-you-use binary needs to be in your path for any of the above to work properly.
By default, iwyu_tool.py is single-threaded, which can be slow for large projects. You can use the --jobs argument to increase the number of source files that will be processed in parallel.
You can also enable it globally outside the cmake script by setting the cmake variable:
cmake -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="iwyu" <builddir>
It will then call it on each CXX target.
I extended the source code from Alastair Harrison, in order to create a reusable solution. I've came up with the following that should work with all CMake versions:
File iwyu.cmake:
#.rst:
# include-what-you-use (iwyu)
# ----------------------------
#
# Allows to run the static code analyzer `include-what-you-use (iwyu)
# <http://include-what-you-use.org>`_ as a custom target with the build system
# `CMake <http://cmake.org>`_.
#
# .. topic:: Dependencies
#
# This module requires the following *CMake* modules:
#
# * ``FindPythonInterp``
#
# .. topic:: Contributors
#
# * Florian Wolters <wolters.fl#gmail.com>
#===============================================================================
# Copyright 2015 Florian Wolters
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#===============================================================================
# ------------------------------------------------------------------------------
# Include guard for this file.
# ------------------------------------------------------------------------------
if(iwyu_included)
return()
endif()
set(iwyu_included TRUE)
option(BUILD_IWYU
"Run the include-what-you-use static analyzer on the source code of the project."
OFF)
function(iwyu_enable)
set(iwyu_EXECUTABLE_NAME include-what-you-use)
find_program(iwyu_EXECUTABLE ${iwyu_EXECUTABLE_NAME})
if(iwyu_EXECUTABLE)
# This is not exactly the same behavior as with CMake v3.3, since here all
# compiled targets are analyzed.
set(iwyu_tool_EXECUTABLE_NAME iwyu_tool.py)
find_package(PythonInterp)
find_program(iwyu_tool_EXECUTABLE ${iwyu_tool_EXECUTABLE_NAME})
if(PYTHONINTERP_FOUND AND iwyu_tool_EXECUTABLE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON PARENT_SCOPE)
add_custom_target(iwyu
ALL
COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_EXECUTABLE}" -p "${CMAKE_BINARY_DIR}"
COMMENT "Running the ${iwyu_tool_EXECUTABLE_NAME} compilation database driver"
VERBATIM)
else()
message(STATUS
"Unable to find the Python interpreter and/or the ${iwyu_tool_EXECUTABLE_NAME} script")
endif()
else()
message(STATUS "Unable to find the ${iwyu_EXECUTABLE_NAME} executable")
endif()
endfunction()
File CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
include(iwyu.cmake)
project(hello_world)
add_executable(${PROJECT_NAME} main.cc)
if(BUILD_IWYU)
iwyu_enable()
endif()
Invoke CMake as follows to run include-what-you-use when the all target is invoked:
cmake -DBUILD_IWYU=ON <path-to-source>
cmake --build . --target all
The output should be as follows:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wolters/workspace/include-what-you-use_example/build
[ 66%] Built target hello_world
[100%] Running the iwyu_tool.py compilation database driver
/home/wolters/workspace/include-what-you-use_example/develop/main.cc should add these lines:
/home/wolters/workspace/include-what-you-use_example/develop/main.cc should remove these lines:
- #include <vector> // lines 1-1
The full include-list for /home/wolters/workspace/include-what-you-use_example/develop/main.cc:
#include <iostream> // for operator<<, basic_ostream, cout, endl, ostream
---
[100%] Built target iwyu
Edit 2015-08-19: The approach with the CMake property <LANG>_INCLUDE_WHAT_YOU_USE did not work for me with CMake version 3.3.1. Therefore I updated the solution.
Edit 2015-09-30: The output was wrong, since my include-what-you-use installation was broken. The include-what-you-use and iwyu_tool.py files have to be in the same directory as clang, clang++, etc.