i have this simple program in the clion editor
//main.cpp
#include "my.h"
int main(){
foo = 1;
print_foo();
return 0;
}
//my.cpp
#include <iostream>
#include "my.h"
void print_foo(){
std::cout << foo << std::endl;
}
//my.h
#ifndef INC_Project
#define INC_Project
extern int foo;
void print_foo();
#endif //INC_Project
cmake_minimum_required(VERSION 3.19)
project(Project)
set(CMAKE_CXX_STANDARD 11)
add_executable(Project my.h my.cpp main.cpp)
i am getting this output
[100%] Linking CXX executable Project
/usr/bin/ld: CMakeFiles/Project.dir/my.cpp.o: warning: relocation against
foo' in read-only section
.text'/usr/bin/ld: CMakeFiles/Project.dir/main.cpp.o: in function `main':
/Project/main.cpp:5: undefined reference to `foo'
/usr/bin/ld: CMakeFiles/Project.dir/my.cpp.o: in function `print_foo()':
/home/o/Documents/cpp/Project/my.cpp:6: undefined reference to `foo'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/Project.dir/build.make:118: Project] Error 1
make[2]: *** [CMakeFiles/Makefile2:95: CMakeFiles/Project.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:102: CMakeFiles/Project.dir/rule] Error 2
make: *** [Makefile:137: Project] Error 2