0

我在 VC++2010 中有一个游戏项目,我正在更新它以使用 SDL,首先使用音频。当我从头开始重建项目时,音频类给了我几个链接器错误。当我去 Audio.cpp 并在任何地方添加一些空白(因此需要再次保存文件)并构建它时编译成功。我觉得这很神秘。我已经重新启动了几次 Visual Studio,但这种行为仍然存在。

我是 C++ 编程的新手;我的大部分经验是使用高级语言(C#、Python)。谢谢你的帮助。

这是音频.h

#pragma once

#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"
#include "SDL/SDL_mixer.h"

#include <string>
#include <vector>

using namespace std;

class AudioTrack {
public:
    string Description;
        string Filename;
        Mix_Music* track;
        AudioTrack(const string& Description, const string& Filename){
                this->Description = Description;
                this->Filename = Filename;
                track = Mix_LoadMUS(Filename.c_str());
        }
};

class AudioEffect {
public:
        string Description;
        string Filename;
        Mix_Chunk* effect;
        AudioEffect(const string& Description, const string& Filename){
                this->Description = Description;
                this->Filename = Filename;
                effect = Mix_LoadWAV(Filename.c_str());
        }
};

class Audio {
private:
        static vector<AudioTrack> tracks;
        static vector<AudioEffect> effects; 

        static void AddTrack(const string& Description, const string& Filename);
        static void AddEffect(const string& Description, const string&  Filename);

public:
        //initializes and loads sound
        static void Init();

        static void Free();

        //plays continuously until there is more music.
        static void PlayTrack(const string& Description);

        //plays once
        static void PlayEffect(const string&  Description);
};

这是 Audio.cpp

#include "Audio.h"
#include <stdio.h>


//void musicDone() {
//  Mix_HaltMusic();
//  Mix_FreeMusic(music);
//  music = NULL;
//} 

vector<AudioTrack> Audio::tracks;
vector<AudioEffect> Audio::effects;  

void Audio::AddTrack(const string& Description, const string& Filename) {
    AudioTrack newTrack = AudioTrack(Description,Filename);
        tracks.push_back(newTrack);
}

void Audio::AddEffect(const string& Description, const string& Filename) {
        AudioEffect newEffect = AudioEffect(Description,Filename);
        effects.push_back(newEffect);
}  

void Audio::Init() {
        //SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
        SDL_Init(SDL_INIT_AUDIO);

        int audio_rate = 22050;
        Uint16 audio_format = AUDIO_S16; 
        int audio_channels = 2;
        int audio_buffers = 4096;

        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
                printf("Unable to open audio!\n");
                exit(1);
        }

        //Descriptions for all tracks and effects
        AddTrack("Title","Title.wav");
        AddTrack("Game","Sample_Track_1_V2.wav");

        AddEffect("Collision","Collision_1.wav");

}

void Audio::Free() {
        //TODO

        //free music


}   

void Audio::PlayTrack(const string& Description) {
        for (int i = 0; i < tracks.size(); i++) {
                if (tracks[i].Description.compare(Description) == 0) {
                        Mix_PlayMusic(tracks[i].track, -1);
                        /*Mix_HookMusicFinished(musicDone);*/
                }
        }
} 

void Audio::PlayEffect(const string& Description) {
        for (int i = 0; i < effects.size(); i++) {
                if (effects[i].Description.compare(Description) == 0) {
                        Mix_PlayChannel(-1,effects[i].effect, 0);
                }
        }
}

以下是我在新版本中遇到的链接器错误:

1>Debug\//Audio.obj : warning LNK4042: object specified more than once; extras ignored
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>Draw.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Audio::PlayTrack(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?PlayTrack@Audio@@SAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "void __cdecl setMode(int)" (?setMode@@YAXH@Z)
1>SampleDraft.obj : error LNK2001: unresolved external symbol "public: static void __cdecl Audio::PlayTrack(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?PlayTrack@Audio@@SAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>SampleDraft.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Audio::Init(void)" (?Init@Audio@@SAXXZ) referenced in function _SDL_main
1>C:\Dev Code\Sample\Sample 2013 Draft\\Bin\SampleDraft.exe : fatal error LNK1120: 3 unresolved externals

这是我在 Audio.cpp 添加一些空格并简单构建后得到的警告。构建成功且功能正常:

1>Debug\//Audio.obj : warning LNK4042: object specified more than once; extras ignored
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>  SampleDraft.vcxproj -> C:\Dev Code\Sample\Sample 2013 Draft\\Bin\SampleDraft.exe

编辑:这可能完全不相关。对于大多数开发,在解决方案资源管理器中,头文件和代码文件位于单独的虚拟文件夹中以供显示。然而,几周前,它们似乎切换为全部显示在一个文件夹中。

4

2 回答 2

2

看起来您输出的第一行试图向您发出有关原因的警告:

1>Debug//Audio.obj : warning LNK4042: object specified不止一次;额外被忽略

显然,链接器正在传递多个 Audio.obj 对象文件。看起来在某个地方的发行版中有一个“陈旧”的(用文件资源管理器环顾四周),它没有定义。当您更新并编译正确的 Audio.cpp 时,会创建一个包含它们的 Audio.obj。

于 2013-07-08T00:37:51.277 回答
2

该解决方案与 Visual Studio 编译器行为有关!(我最初在 2013 年将其作为我的问题的编辑,但根据@Nevo 的评论意识到我应该将其列为实际答案。)

最初,我添加头文件Audio.hAudio.cpp因为在 Add->New Item 对话框中单击错误。我只是重命名了文件并继续编码。但是,Visual Studio 似乎也将文件类型存储在项目文件 (.vcxproj) 中。Visual Studio 仍在尝试编译Audio.h为代码。我通过右键单击Audio.h解决方案资源管理器 -> 常规 -> 项目类型并将其设置为“C/C++ 标头”而不是“C/C++ 编译器”来解决此问题。

项目文件中的更改如下所示:

     <ClCompile Include="Audio.cpp" />
-    <ClCompile Include="Audio.h" />
+    <ClInclude Include="Audio.h" />
于 2018-08-24T00:39:37.537 回答