0

enter image description here

I have started to learn CMake framework to build cmake recently and started a dummy project to understand how it works.

My project structure is as shown in the image. The CMakeLists.txt at the root level (myapp->CMakeLists.txt) is shown below:

cmake_minimum_required(VERSION 3.20)

project(dummyCMake)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra")

set(CMAKE_CXX_FLAGS_RELEASE "-o0")

add_subdirectory(libs)

include_directories(includes)

file(GLOB SOURCES "src/*.cpp")

add_executable(${PROJECT_NAME} ${SOURCES})

get_filename_component(parent_dir ../ ABSOLUTE)

target_link_libraries(${PROJECT_NAME} PUBLIC uiolibs)

target_link_libraries(${PROJECT_NAME} PUBLIC mathlibs)

target_include_directories(${PROJECT_NAME} PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/libs/uiolibs/includes"
"${PROJECT_SOURCE_DIR}/libs/mathlibs/includes"
)

sub directories CMakeLists.txt is as follows:

file(GLOB sources "src/*cpp")
message(${sources}) #for debugging
add_library(uiolibs  STATIC ${sources})

Getting linking error undefined reference to `mathlibs::AddInit(int, int)

undefined reference to `userinput::getInit()

Any suggestion to fix this issue (or in general on cmake framework) will be appreciated.

The project code folder: github link

palas
  • 19
  • 4
  • Are you sure your github code is latest? I'm getting cc1plus: error: too many filenames given. Type cc1plus --help for usage – harry Sep 04 '21 at 16:12
  • @harry yes it is. FYI, steps I am following: 1. Download 2. mkdir build 3. cd build 4. cmake -DCMAKE_BUILD_TYPE=Debug .. 5. make :: throwing unknown reference to AddInt, getInt(). – palas Sep 04 '21 at 17:07

1 Answers1

2

It's good that you linked your code here because it's not a cmake problem.

using namespace userinput;
int getInit() {}

This doesn't define userinput::getInit as you expect, it just defines getInit.

yyyy
  • 593
  • 2
  • 10
  • Thank you @yyyy, it is working. However, my second question would be if I want to using namespace, how would I do? – palas Sep 04 '21 at 17:16
  • @palas You can either put your function in `namespace userinput { ... }` like what you do in the head files, or write `int userinput::getInit() {}` – yyyy Sep 04 '21 at 17:43