Issue with WinSock when adding the AUTOMOC to cmake file - c++

I have a problem while I am writing the cmake file. I have a project which both uses the Qt and WinSock2. But when I add set(CMAKE_AUTOMOC ON) to the CMake code, it gives me these errors:
Error 4 error C2011: 'sockaddr' : 'struct' type
redefinition C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\include\ws2def.h 212 Error 5 error C2059: syntax
error : 'constant' C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\include\ws2def.h 390 Error 6 error C3805:
'constant': unexpected token, expected either '}' or a ',' C:\Program
Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h 390 ...
And my cmake file is as follows:
PROJECT(RFIDVis-core)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
#INCLUDE(../cmake/commonconf.cmake)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
MESSAGE(STATUS "Configuring RFIDVis Source")
# glob sources from core directories
FILE(GLOB RFIDVisSources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
BusinessLogic/*.cpp
DataLayer/*.cpp
IrrlichtComponents/*.cpp
GeneratedFiles/Debug/*.cpp
Libraries/*.cpp
Presentation/*.cpp
main.cpp
)
FILE(GLOB RFIDVisHeaders RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
BusinessLogic/*.h
DataLayer/*.h
Libraries/*.h
Libraries/*.hpp
IrrlichtComponents/*.h
Presentation/*.h
)
FILE(GLOB CampvisApplicationForms
Presentation/*.ui
)
SET(RFIDVisApplicationToBeMocced
GeneratedFiles/ui_demomainwindow.h
)
#
# Qt related stuff:
#
QT5_WRAP_CPP(RFIDVisApplicationMoc ${RFIDVisApplicationToBeMocced})
LIST(APPEND RFIDVisApplicationSources ${RFIDVisApplicationMoc})
QT5_WRAP_UI(RFIDVisApplicationFormsHeaders ${RFIDVisApplicationForms})
LIST(APPEND RFIDVisApplicationSources ${RFIDVisApplicationFormsHeaders})
LINK_DIRECTORIES(${RFIDVisGlobalLinkDirectories} ${IRRLICHT_LIBRARY}
${RFIDVisHome}/ext/VISA/lib/msc/visa32.lib
${RFIDVisHome}/ext/VISA/lib/msc/rsspecan.lib
)
qt5_generate_moc(main.cpp main.moc)
ADD_EXECUTABLE(RFIDVis-application
${RFIDVisSources} ${RFIDVisHeaders} ${RFIDVisMocs}
)
ADD_DEFINITIONS(${RFIDVisGlobalDefinitions})
INCLUDE_DIRECTORIES(${RFIDVisGlobalIncludeDirs})
cmake_policy(SET CMP0020 NEW)
TARGET_LINK_LIBRARIES(RFIDVis-application Qt5::Core Qt5::Widgets ws2_32.lib
${RFIDVisHome}/ext/VISA/lib/msc/visa32.lib
${RFIDVisHome}/ext/VISA/lib/msc/rsspecan.lib
${IRRLICHT_LIBRARY})
qt5_use_modules(RFIDVis-application Core GUI Widgets)
DEFINE_SOURCE_GROUPS_FROM_SUBDIR(RFIDVisSources ${RFIDVisHome} "")
DEFINE_SOURCE_GROUPS_FROM_SUBDIR(RFIDVisHeaders ${RFIDVisHome} "")

The problem is that the windows.h is going to be added to the project after adding the AUTOMOC. So, the solution is to add add_definitions(-DWIN32_LEAN_AND_MEAN) to the cmake file. Then, the windows.h will not include the winsock and you can include it by yourself in your project

Related

Can't build OR-Tools (fetched with FetchContent) with MSVC in Qt Creator

I'm trying to build a project that integrates OR-Tools, with MSCV 2019 in Qt Creator, but there are strange build errors (hundred of them, in Abseil source code at the moment, but probably only because it's built first):
C:\MyProject\build_deps\absl-src\absl\time\time.h:110: erreur : C2039: 'ratio': is not a member of 'std'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\algorithm(40): note: see declaration of 'std'
C:\MyProject\build_deps\absl-src\absl\time\time.h:110: erreur : C2061: syntax error: identifier 'ratio'
C:\MyProject\build_deps\absl-src\absl\time\time.h:408: erreur : C2760: syntax error: unexpected token '{', expected ')'
I'm fetching OR-Tools with FetchContent, with the following CMakeLists.txt (I've removed most of the default Qt boilerplate):
cmake_minimum_required(VERSION 3.15)
project(MyProject VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)
set(PROJECT_SOURCES main.cpp)
qt_add_executable(${PROJECT_NAME}
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
include(FetchContent)
set(BUILD_DEPS ON)
set(BUILD_SAMPLES OFF)
set(BUILD_EXAMPLES OFF)
FetchContent_Declare(
or-tools
GIT_REPOSITORY https://github.com/google/or-tools.git
GIT_TAG v9.3
)
FetchContent_MakeAvailable(or-tools)
target_link_libraries(${PROJECT_NAME} PRIVATE or-tools)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
target_compile_options(${PROJECT_NAME} PRIVATE /experimental:external /external:anglebrackets /external:W0)
qt_finalize_executable(${PROJECT_NAME})
For information, Qt Creator successively calls:
C:\Qt\Tools\CMake_64\bin\cmake.exe -S "C:/MyProject" -B "C:/MyProject/build" "-DCMAKE_GENERATOR:STRING=Ninja" "-DCMAKE_BUILD_TYPE:STRING=Debug" "-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=C:/Qt/Tools/QtCreator/share/qtcreator/package-manager/auto-setup.cmake" "-DQT_QMAKE_EXECUTABLE:FILEPATH=C:/Qt/6.3.0/msvc2019_64/bin/qmake.exe" "-DCMAKE_PREFIX_PATH:PATH=C:/Qt/6.3.0/msvc2019_64" "-DCMAKE_C_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl.exe"
C:\Qt\Tools\CMake_64\bin\cmake.exe --build "C:/MyProject/build" --target all
The first failed compile command is:
C:\MSVC\x64\cl.exe /nologo /TP
-I"C:\MyProject\build\_deps\absl-build\absl\time"
-I"C:\MyProject\build\_deps\absl-src\absl\time"
-I"C:\MyProject\build\_deps\absl-build\absl\time\time_zone_autogen\include"
-I"C:\MyProject\build\_deps\absl-src"
/DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /W3 /DNOMINMAX
/DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS /D_ENABLE_EXTENDED_ALIGNED_STORAGE
/bigobj /wd4005 /wd4068 /wd4180 /wd4244 /wd4267 /wd4503 /wd4800 -std:c++20 /showIncludes
/Fo_deps\absl-build\absl\time\CMakeFiles\time_zone.dir\internal\cctz\src\time_zone_lookup.cc.obj
/Fd_deps\absl-build\absl\time\CMakeFiles\time_zone.dir\time_zone.pdb
/FS -c "C:\MyProject\build\_deps\absl-src\absl\time\internal\cctz\src\time_zone_lookup.cc
Am I missing something?
Can you try with c++17 instead of c++20?
This happens when a project (such as https://github.com/abseil/abseil-cpp, used by or-tools) has a file called time.h.
The project builds fine when built standalone, but when used with FetchContent in a toplevel project that defines set(CMAKE_INCLUDE_CURRENT_DIR ON), <ctime> ends up including the time.h file from the project instead of the time.h file from the system.
I fixed this with
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
FetchContent_MakeAvailable(absl)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

How do I fix a linker error in Clion when there seems to be no problem with the code?

So, I have no code, just empty files and a CMake, but I keep getting that Linker Error. Can someone please explain in a lot of detail what my problem is? Some info I have is that I am supposed to be using Visual Studio 2015 as my compiler and stuff, which I think I already have set up.
The Error:
[100%] Linking CXX executable Debug\CinderGame\CinderGame.exe
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2020.2.3\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
LINK Pass 1: command "C:\PROGRA~2\MICROS~1.0\VC\bin\link.exe /nologo #CMakeFiles\CinderGame.dir\objects1.rsp /out:Debug\CinderGame\CinderGame.exe /implib:CinderGame.lib /pdb:C:\Users\cesar\Documents\GitHub\final-project-cesarmonsalud\cmake-build-debug\Debug\CinderGame\CinderGame.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:windows /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:LIBCPMT -LIBPATH:C:\Users\cesar\Desktop\cinder_0.9.2_vc2015\lib\msw\x86 C:\Users\cesar\Desktop\cinder_0.9.2_vc2015\lib\msw\x86\Debug\v140\cinder.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\CinderGame.dir/intermediate.manifest CMakeFiles\CinderGame.dir/manifest.res" failed (exit code 1120) with the following output:
LIBCMTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
Debug\CinderGame\CinderGame.exe : fatal error LNK1120: 1 unresolved externals
The Code:
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11)
project(Connect4)
# This tells the compiler to not aggressively optimize and
# to include debugging information so that the debugger
# can properly read what's going on.
set(CMAKE_BUILD_TYPE Debug)
# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)
# Let's nicely support folders in IDE's
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Warning flags
if(MSVC)
# warning level 3 and all warnings as errors
add_compile_options(/W3)
else()
# lots of warnings and all warnings as errors
add_compile_options(-Wall -Wpedantic -Werror)
endif()
# FetchContent added in CMake 3.11, downloads during the configure step
include(FetchContent)
# FetchContent_MakeAvailable was not added until CMake 3.14
if(${CMAKE_VERSION} VERSION_LESS 3.14)
include(cmake/add_FetchContent_MakeAvailable.cmake)
endif()
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG devel
)
# Adds Catch2 testing library
FetchContent_GetProperties(catch2)
if(NOT catch2_POPULATED)
FetchContent_Populate(catch2)
add_library(catch2 INTERFACE )
target_include_directories(catch2 INTERFACE ${catch2_SOURCE_DIR}/single_include)
endif()
get_filename_component(CINDER_PATH "C:/Users/cesar/Desktop/cinder_0.9.2_vc2015" ABSOLUTE)
get_filename_component(APP_PATH "${CMAKE_CURRENT_SOURCE_DIR}/" ABSOLUTE)
include("${CINDER_PATH}/proj/cmake/modules/cinderMakeApp.cmake")
list(APPEND CORE_SOURCE_FILES
)
list(APPEND SOURCE_FILES ${CORE_SOURCE_FILES}
include/core/Connect4.h
include/visualizer/FinalProjectApp.h
src/core/Connect4.cpp
src/visualizer/FinalProjectApp.cpp
)
list(APPEND TEST_FILES tests/Connect4Tests.cpp)
ci_make_app(
APP_NAME CinderGame
CINDER_PATH ${CINDER_PATH}
SOURCES apps/cinder_game.cpp ${SOURCE_FILES}
INCLUDES include
)
ci_make_app(
APP_NAME ConsoleGame
CINDER_PATH ${CINDER_PATH}
SOURCES apps/main_console_game.cpp ${SOURCE_FILES}
INCLUDES include
)
ci_make_app(
APP_NAME GameTest
CINDER_PATH ${CINDER_PATH}
SOURCES tests/Connect4Tests.cpp ${SOURCE_FILES} ${TEST_FILES}
INCLUDES include
LIBRARIES catch2
)
if(MSVC)
set_property(TARGET GameTest APPEND_STRING PROPERTY LINK_FLAGS " /SUBSYSTEM:CONSOLE")
endif()
The culprit is probably:
if(MSVC)
set_property(TARGET GameTest APPEND_STRING PROPERTY LINK_FLAGS " /SUBSYSTEM:CONSOLE")
endif()
This tells the linker to build for a console subsystem, which does not have an implicit WinMain function: you'd have to write one yourself if you need a console build.
If you don't need a console build, you can change it to /SUBSYSTEM:WINDOWS and a WinMain will be supplied by MFC.

Cmake - fatal error U1073: don't know how to make

Here is my main CmakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(Vibranium_Core)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost 1.72.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Common)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/WorldServer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/AuthServer)
set_target_properties(
Common WorldServer AuthServer
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
Here is my: /Source/Common/CmakeLists.txt:`
add_library(Common SHARED Config.cpp Config.h Logger.cpp Logger.h)
target_include_directories(Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
When I try to build my project I get:
NMAKE : fatal error U1073: don't know how to make 'bin\lib\Common.lib'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
I have this problem only on Windows. When I try to build the project on Linux there is no issue. What is causing this problem and how can I fix it?

cmake in windows requires all include directories to be lowercase

I am having this weird issue where the paths that I use for my include_directories and target_link_libraries has to be all lowercase.
My project has a subdirectory under it at GLTestBed/src if I try to include it , ninja (or even the built VS solution) throws an error
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ninja : error : '../../GLTestBed/src/Mesh.cpp', needed by 'GLTestBed/CMakeFiles/GlTestBed.dir/src/Mesh.cpp.obj', missing and no known rule to make it
Mesh.cpp is the first file added in my add_Library directive, if I remove that it throws this error for the file that is at top, so I am pretty sure its not an error for just this particular file
However if I change the folder name from GLTestBed to gltestbed every thing builds correctly
CmakeLists.txt in main Folder (GLParticles)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
project(GLParticles)
set (glparticlesdir "${CMAKE_CURRENT_SOURCE_DIR}/GLTestBed")
message ("-----" , ${glparticlesdir})
add_subdirectory(${glparticlesdir})
add_executable(GLParticles
src/main.cpp
src/ParticleWorld.cpp
src/ParticleWorld.h
src/BurstParticleUpdater.cpp
src/BurstParticleUpdater.h src/SineWaveUpdater.cpp src/SineWaveUpdater.h)
include_directories(${glparticlesdir}/src)
include_directories(${glparticlesdir}/ext/glm)
target_link_libraries(GLParticles
GlTestBed)
# SET(CMAKE_CXX_LINK_FLAGS "-framework OpenGL")
and the CmakeLists.txt in the GLTestBed folder
cmake_minimum_required(VERSION 3.10)
project(GlTestBed)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenGL REQUIRED)
#find_package(GLEW REQUIRED)
message ('--------' ${OPENGL_LIBRARIES})
add_Library(GlTestBed
src/Globals.h
src/Mesh.cpp
src/Mesh.h
src/ResourceManager.cpp
src/ResourceManager.hpp
src/Shader.cpp
src/Shader.h
src/stb_image.cpp
src/stb_image.h
src/Material.h
src/Engine.cpp src/Engine.h
src/Particle.cpp
src/Particle.h
src/Camera.h
src/Camera.cpp
src/ParticleManager.cpp
src/ParticleManager.h
src/ParticleUpdater.h)
target_include_directories(GlTestBed PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(GlTestBed PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ext/glm)
target_link_libraries(GlTestBed
${OPENGL_LIBRARIES}
glew32.lib
sfml-graphics.lib
sfml-window.lib
sfml-system.lib)
if(APPLE)
target_include_directories(GlTestBed PUBLIC
/usr/local/include)
target_link_libraries(GlTestBed
/usr/local/Cellar/glew/2.1.0/lib/libGLEW.2.1.0.dylib
/usr/local/Cellar/sfml/2.4.2_1/lib/libsfml-window.2.4.2.dylib
/usr/local/Cellar/sfml/2.4.2_1/lib/libsfml-system.2.4.2.dylib ${OPENGL_LIBRARIES})
endif()

Cmake QT5 linking error

Seems to me that QT5 libraries were not linked correctly when linking them with cmake.
I have a cmake file that set an option to use QT4 or QT5:
if(USE_Qt5)
if(NOT Qt5_PREFIX_PATH)
set(Qt5_PREFIX_PATH /Applications/Qt/5.2.1/clang_64)
endif(NOT Qt5_PREFIX_PATH)
set(WINSDK_LIB_DIR
"C:/Program Files (x86)/Windows Kits/8.0/Lib/win8/um/x64"
"C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib"
)
set(CMAKE_PREFIX_PATH ${3RDPARTY} ${Qt5_PREFIX_PATH} ${WINSDK_LIB_DIR} ${CMAKE_PREFIX_PATH})
message(STATUS "Qt5 prefix path set to: ${CMAKE_PREFIX_PATH}")
find_package(Qt5Widgets)
find_package(Qt5OpenGL)
endif(USE_Qt5)
if(USE_Qt4)
file(GLOB QT4ROOTS ${3RDPARTY}/Qt/*/bin)
find_program(QT_QMAKE_EXECUTABLE NAMES qmake qmake4 qmake-qt4 PATHS ${QT4ROOTS})
find_package(Qt4 REQUIRED)
set(QT_USE_QTOPENGL TRUE)
endif(USE_Qt4)
if qt4 is selected no problems, compile and execute.
Compiling Qt5 return quite a lot of errors that seems to me a linking problem:
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:24: error: variable has incomplete type 'class Q_WIDGETS_EXPORT'
class Q_WIDGETS_EXPORT QSizePolicy
^
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:7: note: forward declaration of 'Q_WIDGETS_EXPORT'
class Q_WIDGETS_EXPORT QSizePolicy
^
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:52:35: error: expected ';' after top level declarator
class Q_WIDGETS_EXPORT QSizePolicy
^
;
/Applications/Qt/5.2.1/clang_64/lib/QtWidgets.framework/Headers/qsizepolicy.h:163:31: error: expected a class or namespace
Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
???
here is were i link the libraries:
if(USE_Qt4 OR USE_Qt5)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
# disable autolinking to qmain
cmake_policy(SET CMP0020 OLD)
set(USE_QT true)
add_definitions(-DWITH_QT)
endif(USE_Qt4 OR USE_Qt5)
if(USE_Qt5)
#target_link_libraries(myProject
# Qt5::Widgets
#)
qt5_use_modules(myProject Widgets)
endif(USE_Qt5)