-3

Im pretty new to multi-file compilation on c++, and I'm trying to understand it by testing. But in the example below I'm getting an error undefined reference to `func()' and I can't understand why. Thanks in advance!

Main.cpp

#include <iostream>
#include "file1.h"

int main() {
    func();
    return 0;
}

File1.h

#ifndef UNTITLED1_FILE1_H
#define UNTITLED1_FILE1_H
#include <iostream>

void func();

#endif //UNTITLED1_FILE1_H

File1.cpp

#include "file1.h"

void func() {
    std::cout << "Test" << std::endl;
}

CMAKELIST

cmake_minimum_required(VERSION 3.17)
project(untitled1)

set(CMAKE_CXX_STANDARD 14)

add_executable(untitled1 main.cpp file1.h)
JVApen
  • 11,008
  • 5
  • 31
  • 67
qwerty156
  • 17
  • 2

1 Answers1

0

Basically you haven't included all your source files. Also you haven't specified your include directory.

cmake_minimum_required(VERSION 3.17)

# Never name your project the same thing as any of your targets
project(COOL_PROJECT)

set(CMAKE_CXX_STANDARD 14)

add_executable(untitled1)

# You must include all source files, header files are optional.
# However adding header files explicitly will result in better IDE support
#
# Prefer to add source files using target_sources instead
target_sources(untitled1 PRIVATE
    main.cpp
    file1.cpp
    file1.h
)

# Add your current directory as an include directory.
target_include_directories(untitiled PRIVATE ${CMAKE_CURRENT_LIST_DIR})

Btw casing matters. So make sure the filenames casing match up.

File1.cpp != file1.cpp