链接源文件与静态库C ++时出错

时间:2012-12-21 14:56:06

标签: c++ compiler-errors static-libraries

我正在尝试使用静态库但是当我向项目中添加引用和包含目录时,我得到了奇怪的编译器错误,这只包括任何头文件。

静态库项目构建成功。我不知道错误的位置,但我想它可能在静态库的某个文件中。

静态库文件如下:

头:

#ifndef UTIL_H_
#define UTIL_H_

#include <string>
#include <iostream>
#include <vector>
#include <stdlib.h>

namespace Kaczmarz {

class Util {
public:
    Util();
    static void split(std::vector<std::string> &tokens, const std::string &text, char sep);
    static double random(int rangeMin,int rangeMax);

    virtual ~Util();
};

} 
#endif

CPP:

#include "Util.h"

namespace Kaczmarz {

Util::Util() {
    // TODO Auto-generated constructor stub
}

void Util::split(std::vector<std::string> &tokens, const std::string &text, char sep) {
    unsigned int start = 0, end = 0;
    while ((end = text.find(sep, start)) != std::string::npos) {
        tokens.push_back(text.substr(start, end - start));
        start = end + 1;
    }
    tokens.push_back(text.substr(start));
}

double Util::random(int rangeMin,int rangeMax){
    return (double) static_cast<double>( rand() ) * rangeMax / static_cast<double>( RAND_MAX ) + rangeMin;
}


Util::~Util() {
    // TODO Auto-generated destructor stub
}

}

尝试使用静态库的文件:

#include <iostream>

using namespace std;
//using namespace Kaczmarz;

int main(){
    cout << "Started.." << endl;
    return 0;
}

请注意,我还没有从库中调用任何函数。

我得到的错误如下:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(38): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(38): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C2086: 'int std::basic_string' : redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21) : see declaration of 'std::basic_string'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(49): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(66): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(66): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C2086: 'int std::basic_string' : redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21) : see declaration of 'std::basic_string'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(77): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(94): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(94): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C2086: 'int std::basic_string' : redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21) : see declaration of 'std::basic_string'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(101): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(114): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(114): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C2086: 'int std::basic_string' : redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21) : see declaration of 'std::basic_string'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(125): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(138): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(138): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C2143: syntax error : missing ';' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C2433: 'basic_string' : 'inline' not permitted on data declarations
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C2086: 'int std::basic_string' : redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(21) : see declaration of 'std::basic_string'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(145): error C2059: syntax error : '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(158): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(158): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(166): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(166): error C2143: syntax error : missing ',' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(170): error C2803: 'operator ==' must have at least one formal parameter of class type
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(170): error C2805: binary 'operator ==' has too few parameters
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(177): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(177): error C2143: syntax error : missing ',' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(180): error C2803: 'operator ==' must have at least one formal parameter of class type
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(186): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(186): error C2143: syntax error : missing ',' before '<'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\string(190): fatal error C1903: unable to recover from previous error(s); stopping compilation

请注意,错误提到了std字符串类。

2 个答案:

答案 0 :(得分:4)

由于是圣诞节,我发现你正在使用MSVS,以下是我尝试使用你的库的步骤:

第1步:我创建了一个名为XmasTest的新解决方案,其中包含名为XmasLib的新Win32项目。

第2步:我已在该项目中添加了源文件,只是一个简单的示例相应修改。我甚至在头文件中放了那个邪恶的using namespace std;行。

Util.h

#ifndef UTIL_H_
#define UTIL_H_

#include <string>
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

namespace Kaczmarz {

class Util {
public:
    Util();
    static void print();

    virtual ~Util();
};

} /* namespace Kaczmarz */
#endif

Util.cpp

#include "Util.h"
using namespace std;

namespace Kaczmarz {

Util::Util() {
    // TODO Auto-generated constructor stub

}

void Util::print() {
    cout << "Util::print() works! yay!" << endl;
}

Util::~Util() {
}

} /* namespace Kaczmarz */

第3步:我创建了名为XmasLibTestApp的新Win32控制台应用程序,其中包含以下代码:

#include <iostream>

#include "../XmasLib/Util.h"

using namespace std;
using namespace Kaczmarz;

int main(){
    Util u;
    u.print();
    return 0;
}

第4步:由于这些是1个解决方案中的2个项目,因此我按以下方式处理了依赖项:

  1. Linker-&gt; General-&gt;其他图书馆目录:$(OutDir)
  2. 链接器 - &gt;输入 - &gt;其他依赖项:XmasLib.lib
  3. 解决方案属性 - &gt; ProjectDependencies:应用程序依赖于lib
  4. 第5步:构建解决方案并运行应用。输出:Util::print() works! yay!

    就是这样。故事的结尾,一切正常,开发人员对他的IDE感到高兴。

    快乐的圣诞节! :d


    PS:值得一看的问题:
    Why is "using namespace std" considered bad practice?
    What requires me to declare "using namespace std;"?
    where to put using namespace std;

答案 1 :(得分:0)

好的找到了这个编译错误的原因。

在“其他包含目录”(vs 2012)中,我有另一个导入“Util”类的.cpp / .h文件。

我刚从目录中删除了这些文件,这次编译好了没有任何错误。请注意,这个文件不存在于项目中(我已将它们排除在外),但似乎“其他包含目录”在项目中以某种方式将它们链接起来。

感谢所有回答的人,我真的很感激。