G ++ - 4.9使用Makefile编译时链接错误而不是OSX上的Xcode

时间:2015-01-12 07:08:09

标签: c++ xcode makefile linker gcc4.9

我有一个C ++库项目,我已经工作了一段时间,现在我已经从舒适的Xcode编写了。我决定将项目设置为使用make进行编译,以便在时机成熟时更容易分发。该项目目前有大约40个标题和30个.cpp文件。

错误:

Undefined symbols for architecture x86_64:
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      void std::__1::vector<DSG::SignalProcess*, std::__1::allocator<DSG::SignalProcess*> >::__push_back_slow_path<DSG::SignalProcess* const&>(DSG::SignalProcess* const&) in AudioSettings.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [lib/DSG.dylib] Error 1

Makefile:

.PHONY : clean install

CC = g++-4.9
CPPFLAGS= -fPIC -g -std=gnu++11 -Os -Wall -I/usr/local/include 
LDFLAGS= -dynamiclib 


SOURCES = $(wildcard src/*.cpp)
HEADERS = $(wildcard src/*.h)
OBJECTS = $(SOURCES:.cpp=.o)

DSG_LIBDIR = lib

DSG_HEADERINSTALLDIR = /usr/local/include/DSG
DSG_LIBINSTALLDIR = /usr/local/lib

TARGET=$(DSG_LIBDIR)/DSG.dylib

all: $(TARGET)

clean:
    rm -f $(OBJECTS) $(TARGET)

install: 
    cp -f $(TARGET) $(DSG_LIBINSTALLDIR) 
    mkdir -p $(DSG_HEADERINSTALLDIR)
    cp -f $(HEADERS) $(DSG_HEADERINSTALLDIR)

$(TARGET): $(OBJECTS)
    $(CC) $(CPPFLAGS) $(OBJECTS) -o $@ $(LDFLAGS)

标题:

#ifndef __DSG__AudioSettings__
#define __DSG__AudioSettings__
#include <vector>
#include "DSGTypes.h"
namespace DSG {
    class SignalProcess;
    /*!\brief DSG::AudioSettings - Global Storage For Audio Settings Such As Sample Rate
     */
    class AudioSettings{
    public:
        static DSG::DSGFrequency const& SampleRate();
        static DSG::DSGFrequency const& SampleRate(DSG::DSGFrequency const& value);
        static DSG::DSGFrequency const& Nyquist();
        static bool AddSampleRateListener(SignalProcess* listener);
        static bool const& IsSampleRateSet();
    protected:
        static DSG::DSGFrequency _sampleRate;
        static DSG::DSGFrequency _nyquist;
        static std::vector<DSG::SignalProcess*> _listeners;
        static bool _set;
    };
    namespace{
#define SampleRateDefault 44100//hidden macro defining default sample rate
    }
    //!\brief DSG::SampleRate - Get Global Sample Rate
    inline DSG::DSGFrequency const& SampleRate(){
        return DSG::AudioSettings::SampleRate();
    }
    //!\brief DSG::SampleRate - Set Global Sample Rate
    inline DSG::DSGFrequency const& SampleRate(DSG::DSGFrequency const& value){
        return DSG::AudioSettings::SampleRate(value);
    }
    //!\brief DSG::Nyquist() - Pre-Calculated Nyquist Limit. Use instead of calculating each time needed. This value will be updated whenever the sample rate changes.
    inline DSG::DSGFrequency Nyquist(){
        return DSG::AudioSettings::Nyquist();
    }
    //!\brief DSG::AddSampleRateListener() - Allows Generators to be notified if the sample rate changes
    inline bool AddSampleRateListener(DSG::SignalProcess* listner){
        return AudioSettings::AddSampleRateListener(listner);
    }
    //!\brief DSG::VerifySampleRateSet() - Allows a Generator to ask if a valid sample rate has been set
    inline void VerifySampleRateSet(){
        if (!DSG::AudioSettings::IsSampleRateSet()) {
            SampleRate(SampleRateDefault);
        }
    }
}
#endif /* defined(__DSG__AudioSettings__) */

和.cpp:

#include "AudioSettings.h"
#include "SignalProcess.h"
DSG::DSGFrequency DSG::AudioSettings::_sampleRate;
DSG::DSGFrequency DSG::AudioSettings::_nyquist;
bool DSG::AudioSettings::_set=false;
std::vector<DSG::SignalProcess*> DSG::AudioSettings::_listeners;
DSG::DSGFrequency const& DSG::AudioSettings::SampleRate(){
    return _sampleRate;
}
DSG::DSGFrequency const& DSG::AudioSettings::SampleRate(DSG::DSGFrequency const& value){
    if (!_set) {
        _set=true;
    }
    _sampleRate = value;
    _nyquist = _sampleRate*0.5;
    for (auto i:_listeners) {
        i->SampleRateChanged(_sampleRate);
    }
    return _sampleRate;
}
DSG::DSGFrequency const& DSG::AudioSettings::Nyquist(){
    return _nyquist;
}
bool DSG::AudioSettings::AddSampleRateListener(DSG::SignalProcess* listener){
    _listeners.push_back(listener);
    return true;
}
bool const& DSG::AudioSettings::IsSampleRateSet(){
    return _set;
}

上面的代码是链接器说它可以从。

中找到符号的源文件

问题是我可以使用Xcode编译并将此源链接到库中,但不能使用makefile。所以我担心我以某种方式弄乱了makefile。我在两种情况下都使用完全相同的编译器,GCC 4.9.2所以我认为这是Xcode和makefile之间不同的编译器和链接器命令的问题。

我需要知道要改变什么才能让它链接起来?如果有更多信息我可以提供,请告诉我。感谢。

0 个答案:

没有答案