0

I need to compile one executable including all the source files inside the client and the common directories

\root
 \client
   *.cpp
   *.h
   CMakeLists.txt
 \common
   *.cpp
   *.h

Here is my current CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0)

project(Client)

set(common_dir ${PROJECT_SOURCE_DIR}/common)

include_directories(${common_dir})


set(CMAKE_CXX_STANDARD 17)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

#set(CMAKE_BUILD_TYPE RELEASE)
if (CMAKE_BUILD_TYPE STREQUAL "RELEASE")
    add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif (CMAKE_BUILD_TYPE STREQUAL "RELEASE")


if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

IF(WIN32)
    SET(OS_SPECIFIC_LIBS netapi32 wsock32)
ENDIF(WIN32)


find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Gui REQUIRED)
find_package(Qt5 COMPONENTS Network REQUIRED)
#find_package(Qt5 COMPONENTS Sql REQUIRED)
find_package(Qt5 COMPONENTS Svg REQUIRED)
find_package(Qt5 COMPONENTS PrintSupport REQUIRED)
find_package(Qt5WebSockets REQUIRED)

file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")
add_library(common_src)

add_executable(Client ${common_src} ${client_src})

target_link_libraries(Client ${common_dir} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network Qt5::Svg Qt5::PrintSupport Qt5::WebSockets ${OS_SPECIFIC_LIBS})

But I'm getting this error:

mingw32-make.exe[3]: *** No rule to make target '../common', needed by 'Client.exe'.  Stop.
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:126: CMakeFiles/Client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:133: CMakeFiles/Client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:150: Client] Error 2
Antonio Santoro
  • 827
  • 1
  • 11
  • 29

2 Answers2

1

Either

remove add_library(common_src)

or

add_library(MyLib ${common_src})
add_executable(Client ${client_src])
target_link_libraries(Client MyLib)
StPiere
  • 4,113
  • 15
  • 24
  • I'm getting undefined references in all the classes inside the client folder that are using classes from the common folder – Antonio Santoro Aug 19 '20 at 12:05
  • that means the library is not properly linked if you are building libary from common_src. Go Step By Step. You can first try to build library only with add_library and make sure that it succeeds. Basicaly one of these 3 scenarios are possible: 1. you forgot to link the library 2. the library can't be found 3. he library itself can not build, because of errors. You have to figure it out. – StPiere Aug 19 '20 at 12:16
  • I'm not building a library from common_src – Antonio Santoro Aug 19 '20 at 12:17
  • then try to print ${common_src} for ex. via message() and make sure that all sources are found - maybe some subdirectories present ?. It can also be that you are using namespaces and looking for symbol in wrong namespace. Just idea. – StPiere Aug 19 '20 at 12:20
  • How can I print that using message()? There are no sub directories inside common – Antonio Santoro Aug 19 '20 at 12:35
1

It seems like you have mixed up quite a few things in your cmake. Here you glob the sources (globbing is bad, don't do it):

file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")

Then you create a library out of common_src:

add_library(common_src)

And then you create an executable with both client_src and common_src:

add_executable(Client ${common_src} ${client_src})

And then you try to link Client with common_dir. This last step doesn't make sense because Client already has the common_src in it and there is no need to link the common_dir library to it.

Hence you can simplify your cmake and do the following (only the relevant part is shown):

#...
file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")
# add_library(common_src) # Remove this line

add_executable(Client ${common_src} ${client_src}) # create Client out of common and client src

target_link_libraries(Client Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network Qt5::Svg Qt5::PrintSupport Qt5::WebSockets ${OS_SPECIFIC_LIBS})

Side notes:

Both of these do essentially the same thing:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # remove this

Instead of the above, I recomment using set_target_properties to set the C++ standard instead:

set_target_properties(Client PROPERTIES
    CXX_STANDARD 17           # standard version
    CXX_STANDARD_REQUIRED ON  # required yes
)
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • Now I get undefined references in all the classes inside the client folder that are using classes from the common folder. – Antonio Santoro Aug 19 '20 at 11:59
  • That's probably because it is unable to find `common_dir`. Use `message(${common_dir})` to print out the dir path and see if it is correct. – Waqar Aug 19 '20 at 12:46
  • It prints nothing, so is ${PROJECT_SOURCE_DIR}/common wrong I guess? – Antonio Santoro Aug 19 '20 at 13:15
  • Yes, I just saw your dir structure again, seems like you need to do `set(common_dir ../common)`. ${PROJECT_SRC_DIR} will not work because you are in the project source dir already i.e., `client/` is the project source directory. – Waqar Aug 19 '20 at 13:21
  • or you can do `${PROJECT_SOURCE_DIR}/../common` – Waqar Aug 19 '20 at 13:22