0

I have a simple C++ project with following files

├── include
│   └── project_name
│       └── node.h
├── src
│   ├── main.cpp
│   └── node.cpp
├── test
└── CMakeLists.txt

and in CmakeLists.txt, I have

cmake_minimum_required(VERSION 3.15)

project(project_name)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# include files
include_directories(
    include
)

# target
add_executable(
    maple
    src/node.cpp
    src/main.cpp
)

As you see. it is very simple project but I was getting

[ 33%] Linking CXX executable maple
Undefined symbols for architecture x86_64:
  "Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::print()", referenced from:
      _main in main.cpp.o
  "Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Node(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [maple] Error 1
make[1]: *** [CMakeFiles/project_name.dir/all] Error 2
make: *** [all] Error 2

The three questions I wanted to ask are

  1. What do I need to change in CMakeLists.txt to fix the issue?

  2. How can I check which C++ compiler is being used?

  3. Is it actually being compiled with C++17? How can I check?

Thank you so much for your time

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Brandon Lee
  • 695
  • 1
  • 10
  • 22
  • 1
    Although you have not shown your code, from the error message I think your bug is here: `src/node.cpp` If node is a template delete node.cpp and move its implementation to the header. [https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file/495056#495056](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file/495056#495056) – drescherjm Mar 13 '20 at 02:22
  • Most probably the error are in your C++ sources. As for bullet `3` `How can I check?` - you reconfigure cmake with `CMAE_VERBOSE_MAKEFILE=TRUE` or if you use `make` do `make VERBOSE=true` – KamilCuk Mar 13 '20 at 02:29

0 Answers0