I am trying to nest code under specific namespace
for organizational purposes. Below is an MVP, I cannot spot the issue in my code. Not sure how you define this nesting or export it to CMakeLists.txt
and Linking. It happens only if a wrap the class inside namespaces
.
demo.h:
#pragma once
#include "../../third-party/fmt/include/fmt/core.h"
#include "../../third-party/fmt/include/fmt/color.h"
namespace demo {
namespace app {
class ApplicationDemo{
public:
void entry();
};
}
}
demo.cpp:
#include "demo.h"
namespace demo
{
namespace app {
void ApplicationDemo::entry(){
std::string m_ascii = R"(
____
| \ ___ _____ ___
| | || -_|| || . |
|____/ |___||_|_|_||___|
)";
fmt::print(fmt::emphasis::bold | fg(fmt::color::orange_red), m_ascii);
}
}
}
main.cpp:
#include <iostream>
#include "demoapp/demo.h"
int main(int argc, char const *argv[])
{
demo::app::ApplicationDemo m_demo;
m_demo.entry();
std::cin.get();
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
project(demo)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} \
-Wall \
-fdiagnostics-color=always \
-mavx")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include_directories(./)
add_subdirectory(third-party/fmt EXCLUDE_FROM_ALL)
target_compile_options(fmt PRIVATE -w)
add_subdirectory(src/demoapp)
set(third_parties fmt)
set(SOURCES src/main.cpp)
add_executable(demo src/main.cpp)
target_link_libraries(demo ${third_parties})
Build output:
[main] Building folder: experiment
[build] Starting build
[proc] Executing command: chcp
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/GitHub/experiment/build --config Debug --target all -j 10 --
[build] [ 20%] Building CXX object third-party/fmt/CMakeFiles/fmt.dir/src/format.cc.obj
[build] [ 40%] Building CXX object third-party/fmt/CMakeFiles/fmt.dir/src/os.cc.obj
[build] [ 60%] Linking CXX static library ..\..\lib\libfmtd.a
[build] [ 60%] Built target fmt
[build] [ 80%] Building CXX object CMakeFiles/demo.dir/src/main.cpp.obj
[build] [100%] Linking CXX executable bin\demo.exe
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\demo.dir/objects.a(main.cpp.obj): in function `main':
[build] C:/GitHub/experiment/src/main.cpp:8:(.text+0x1c): undefined reference to `demo::app::ApplicationDemo::entry()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\demo.dir\build.make:100: bin/demo.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:98: CMakeFiles/demo.dir/all] Error 2
[build] mingw32-make: *** [Makefile:135: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/GitHub/experiment/build --config Debug --target all -j 10 -- exited with code: 2
[driver] Build completed: 00:00:04.365
[build] Build finished with exit code 2
EDIT: I put things like this,
/src/demoapp/
CMakeLists.txt,
demo.cpp
demo.h
/src/main.cpp
The CMakeLists.txt of demoapp:
add_library(demoapp
demo.cpp)
target_link_libraries(demoapp fmt)
if(WIN32)
target_link_libraries(demoapp)
else()
target_link_libraries(demoapp stdc++fs)
endif()
Then, I added this line add_subdirectory(src/demoapp)
to the parent root CMakeLists.txt. But, when I do this, which is my desired goal. Sorry I didn't specify this earlier, when I receive the same error as above. Whereas, if I do the way you showed me, then it works brilliantly.
I appreciate any valuable information on this. Thanks in advance.