VS code Undefined symbols for architecture arm64

huangapple go评论66阅读模式
英文:

VS code Undefined symbols for architecture arm64

问题

这是完整的错误信息:

Undefined symbols for architecture arm64:
  "ug::UgWindow::UgWindow(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main.cpp.o
  "ug::UgWindow::~UgWindow()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled-game] Error 1
make[1]: *** [CMakeFiles/untitled-game.dir/all] Error 2
make: *** [all] Error 2

我正在使用CMake来构建我的文件,我已经创建了一个UgWindow的hpp文件和cpp文件来定义它的功能,以下是hpp文件和cpp文件。

HPP:

#pragma once

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <string>

namespace ug {

    class UgWindow{
        public:
            UgWindow(int w, int h, std::string name);
            ~UgWindow();

            bool shouldClose(){ return glfwWindowShouldClose(window); }
        private:
            void initWindow();

            const int width;
            const int height;
            std::string windowName;
            GLFWwindow *window;
    };
}

以下是cpp文件:

#include "ug_window.hpp"

namespace ug {

UgWindow::UgWindow(int w, int h,  std::string name): width{w}, height{h}, windowName{name} {
    initWindow();
} 

UgWindow::~UgWindow() {
    glfwDestroyWindow(window);
    glfwTerminate();
}

void UgWindow::initWindow() {
    glfwInit();

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
}

}

我正在尝试在glfw和glm中使用Vulkan,我对C ++一窍不通,完全不知道如何修复这个问题或问题是什么。

另外,这是我的CMake文件:

project(untitled-game)
cmake_minimum_required(VERSION 3.22.4)

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

add_executable(${PROJECT_NAME} main.cpp)

find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)

if (VULKAN_FOUND)
    message(STATUS "Found Vulkan, Including and Linking now")
    include_directories(${Vulkan_INCLUDE_DIRS})
    target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)
英文:

Here is the complete error:

Undefined symbols for architecture arm64:
  &quot;ug::UgWindow::UgWindow(int, int, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt;)&quot;, referenced from:
      _main in main.cpp.o
  &quot;ug::UgWindow::~UgWindow()&quot;, referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [untitled-game] Error 1
make[1]: *** [CMakeFiles/untitled-game.dir/all] Error 2
make: *** [all] Error 2

I am using cmake to build my files, i have created a UgWindow hpp file and cpp file to define its functionality here is the hpp file and cpp file.

HPP

#pragma once

#define GLFW_INCLUDE_VULKAN
#include &lt;GLFW/glfw3.h&gt;

#include &lt;string&gt;

namespace ug {
    
    class UgWindow{
        public:
            UgWindow(int w, int h, std::string name);
            ~UgWindow();

            bool shouldClose(){ return glfwWindowShouldClose(window); }
        private:
            void initWindow();

            const int width;
            const int height;
            std::string windowName;
            GLFWwindow *window;
    };
}

here is the cpp file:

#include &quot;ug_window.hpp&quot;

namespace ug {

UgWindow::UgWindow(int w, int h,  std::string name): width{w}, height{h}, windowName{name} {
    initWindow();
} 

UgWindow::~UgWindow() {
    glfwDestroyWindow(window);
    glfwTerminate();
}

void UgWindow::initWindow() {
    glfwInit();

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
}

}

I am trying to use vulkan with glfw and glm, im a noob with c++ and have absolutely no idea how to fix this or what the problem is.

Also this is my cmake file:

project(untitled-game)
cmake_minimum_required(VERSION 3.22.4)


set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -O3 -std=c++17  &quot; )

add_executable(${PROJECT_NAME} main.cpp)

find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)

if (VULKAN_FOUND)
    message(STATUS &quot;Found Vulkan, Including and Linking now&quot;)
    include_directories(${Vulkan_INCLUDE_DIRS})
    target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)

答案1

得分: 2

add_executable(${PROJECT_NAME} main.cpp)

在这里,你说可执行程序将从main.cpp源文件构建,仅限于 main.cpp源文件。

你需要列出所有的源文件:

add_executable(${PROJECT_NAME} main.cpp ug_window.cpp)
英文:
add_executable(${PROJECT_NAME} main.cpp)

Here you say that the executable program is to be built from the main.cpp source file, and only the main.cpp source file.

You need to list all your source files:

add_executable(${PROJECT_NAME} main.cpp ug_window.cpp)

huangapple
  • 本文由 发表于 2023年2月16日 15:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定