Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将SDL2与Cmake/Linux/C+一起使用时未定义的引用+;_C++_Linux_Build_Cmake_Sdl - Fatal编程技术网

C++ 将SDL2与Cmake/Linux/C+一起使用时未定义的引用+;

C++ 将SDL2与Cmake/Linux/C+一起使用时未定义的引用+;,c++,linux,build,cmake,sdl,C++,Linux,Build,Cmake,Sdl,你好, 我试图在Linkux的C++项目中使用SDL2,但对核心函数有未定义的引用。我包括了头文件并正确设置了CmakeLists(我想),所以我不明白他为什么找不到这些函数 我的C++代码: #include "SDL.h" #include "SDL_mixer.h" #include "SDL_image.h" #include <iostream> using namespace std; #define NUM_WAVEFORMS 2 const char* _wave

你好,

我试图在Linkux的C++项目中使用SDL2,但对核心函数有未定义的引用。我包括了头文件并正确设置了CmakeLists(我想),所以我不明白他为什么找不到这些函数

<>我的C++代码:

#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"

#include <iostream>
using namespace std;

#define NUM_WAVEFORMS 2
const char* _waveFileNames[] = {"Kick-Drum-1.wav", "Snare-Drum-1.wav"};

Mix_Chunk* _sample[2];

// Initializes the application data
int Init(void) {
    memset(_sample, 0, sizeof(Mix_Chunk*) * 2);

    // Set up the audio stream
    int result = Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512);
    if( result < 0 ) {
        fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
        exit(-1);
    }

    result = Mix_AllocateChannels(4);
    if( result < 0 ) {
        fprintf(stderr, "Unable to allocate mixing channels: %s\n", SDL_GetError());
        exit(-1);
    }

    // Load waveforms
    for( int i = 0; i < NUM_WAVEFORMS; i++ ) {
        _sample[i] = Mix_LoadWAV(_waveFileNames[i]);
        if( _sample[i] == NULL ) {
            fprintf(stderr, "Unable to load wave file: %s\n", _waveFileNames[i]);
        }
    }

    return true;
}

int main() {
    bool retval = Init();
    cout << retval << endl;
    return 0;
}
My CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)

project (SDL_Test)

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")

file (GLOB source_files "${source_dir}/*.cpp")

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable (SDL_Test ${source_files})
target_link_libraries(SDL_Test ${SDL2_LIBRARIES})

提前谢谢你的帮助

SDL_Mixer附带一个pkg配置文件,因此您可以使用CMake的pkg配置支持:

include(FindPkgConfig)
pkg_check_modules(SDL2_Mixer REQUIRED IMPORTED_TARGET SDL2_mixer)
target_link_libraries(SDL_Test PkgConfig::SDL2_Mixer)

PkgConfig::SDL2_混频器将使用正确的include和linker路径进行预配置。

SDL_混频器附带pkg配置文件,因此您可以使用CMake的pkg配置支持:

include(FindPkgConfig)
pkg_check_modules(SDL2_Mixer REQUIRED IMPORTED_TARGET SDL2_mixer)
target_link_libraries(SDL_Test PkgConfig::SDL2_Mixer)

PkgConfig::SDL2_混频器将使用正确的包含和链接器路径进行预配置。

SDL_混频器是来自SDL2的。我不知道,抱歉。但是,当我试图将其链接到CMakeLists.txt中时,出现了一个错误:CMakeLists.txt中的CMake错误:17(find_package):通过在CMake_模块_路径中不提供“FindSDL_MIXER.CMake”,该项目要求CMake找到“SDL2_MIXER”提供的包配置文件,但CMake没有找到。找不到“SDL_MIXER”提供的具有以下任何名称的包配置文件:SDL_MIXERConfig.cmake SDL_MIXER-config.cmakedl_MIXER是来自SDL2的。我不知道,抱歉。但是,当我试图将其链接到CMakeLists.txt中时,出现了一个错误:CMakeLists.txt中的CMake错误:17(find_package):通过在CMake_模块_路径中不提供“FindSDL_MIXER.CMake”,该项目要求CMake找到“SDL2_MIXER”提供的包配置文件,但CMake没有找到。找不到“SDL_MIXER”提供的具有以下任何名称的包配置文件:SDL_MIXERConfig.cmake SDL_MIXER-config.cmake