简体   繁体   中英

Can't build c++ project in CLion

I have a hello-wrold project:
main.cpp:

#include <iostream>
#include "square.h"
    int main() {
        int z = 5;
        std::cout << square(z) << std::endl;
        return 0;
    }

square.cpp:

int square(int x)
{
    return x * x;
}

square.h:

#ifndef SQUARE_H
#define SQUARE_H

int square(int x);

#endif //SQUARE_H

It compiles and runs with g++ from terminal:

g++ -c main.cpp
g++ -c square.cpp
g++ main.o square.o -o programm
./programm
>25

But the CLion's build end up with:

[ 50%] Linking CXX executable cpp_code
Undefined symbols for architecture x86_64:
  "square(int)", 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[3]: *** [cpp_code] Error 1
make[2]: *** [CMakeFiles/cpp_code.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp_code.dir/rule] Error 2
make: *** [cpp_code] Error 2

PS I used to write simple c++ programms for a long time(with big pauses though), and this is the first time, im facing problems at this step. Also, there's a CMakeList.txt automaticly created(in VisStud there was nothing similar!) CMakeList.txt:

cmake_minimum_required(VERSION 3.12)
project(cpp_code)

set(CMAKE_CXX_STANDARD 14)

add_executable(cpp_code main.cpp)

add_executable expects the list of files that need to be compiled to create said executable. Add square.cpp to that list.

add_executable(cpp_code main.cpp square.cpp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM