1

im trying to use CMake for the first time to compile a c++ file.

test.cpp :

#include <iostream>

int main(){

   std::cout << "Hello World" << std::endl;

   return 0;
}

CMakeLists.txt :

cmake_minimum_required(VERSION 3.0)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

project(Void)

add_executable(
    output
    tests/test.cpp   
)

i made the Makefile (Compiler : GNU) successfully, but when using the make command to compile, i get these errors

output :

Consolidate compiler generated dependencies of target output

[ 50%] Linking CXX executable output

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj:C:/Users/admin/Desktop/SharedWork/C++/Void/tests/test.cpp:5: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj: in function main':

C:/Users/admin/Desktop/SharedWork/C++/Void/tests/test.cpp:5: undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj:C:/msys64/mingw64/include/c++/10.3.0/iostream:74: undefined reference to std::ios_base::Init::~Init()'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj:C:/msys64/mingw64/include/c++/10.3.0/iostream:74: undefined reference to std::ios_base::Init::Init()'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj:test.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/output.dir/tests/test.cpp.obj:test.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to std::cout'

collect2.exe: error: ld returned 1 exit status

make[2]: *** [CMakeFiles/output.dir/build.make:97 : output] Erreur 1

make[1]: *** [CMakeFiles/Makefile2:83 : CMakeFiles/output.dir/all] Erreur 2

make: *** [Makefile:91 : all] Erreur 2
Skks
  • 31
  • 4
  • 4
    Does this answer your question? [undefined reference to 'std::cout'](https://stackoverflow.com/questions/28236870/undefined-reference-to-stdcout) – ewokx May 10 '21 at 00:15
  • no sorry, it didn't help me – Skks May 10 '21 at 00:27
  • 3
    This doesn't look like a cmake issue, but rather an issue with configuring a Windows port of a Linux C++ compiler. The ported gcc can't find the runtime C++ library, too link with. Even experienced developers will struggle with shoehorning gcc into Windows, not to mention those who are new to C++. Most of the time it takes much less time to simply install Linux from scratch, and get a fully working, fully configured, modern C++ compiler. It took me just an hour and a half, last time. Now, I'd just buy a Lenovo laptop with preloaded Linux. How much time was already spend on this? – Sam Varshavchik May 10 '21 at 00:36
  • i'd say 2-3 hours, i've been using g++ or clang++ in command line to compile my code but now im working on a bigger project and i need to use cmake, and i've been trying to figure out how to fix this issue for the past 3 hours at least, also i had everything working before with clang++ and g++ but i removed everything from the PC because it was a huge mess and i didn't configure it right from the start, so i did everything from scratch but this time using msys2. – Skks May 10 '21 at 00:41
  • Try `project(Void CXX)`. If that doesn't help, try `set_target_properties(output PROPERTIES LINKER_LANGUAGE CXX)` – dabo42 May 10 '21 at 01:24
  • 1
    Don't set `CMAKE_CXX_FLAGS` or `CMAKE_BUILD_TYPE` in the CMakeLists.txt. The latter is meant to be set at the command line. For what you're doing with the former, you should instead add `target_compile_features(output PRIVATE cxx_std_17)` to the end of the file. With _very, **very**_ few exceptions, you should not put any code in between `cmake_minimum_required` and `project` (i.e. they should be the first two lines). – Alex Reinking May 10 '21 at 02:08
  • When run `make` for build your project, use additional `VERBOSE=1` option. That way Make will show you exact command lines it executes. Check, that command lines used for compile and link your executable *looks* correct. You could also run these command lines to reproduce the error without CMake/Make. If unsure, add these command lines into the question post. – Tsyvarev May 10 '21 at 07:58
  • This is not a duplicate as the problem is with cmake not using c++ on some platforms such as mingw/msys2. Can be fixed by putting this in CMakeLists.txt: SET(CMAKE_CXX_COMPILER c++) – lele1c Dec 21 '21 at 06:14

0 Answers0