0

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

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

静态库文件如下:

标题:

#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

cp:

#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 字符串类。

4

2 回答 2

4

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

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

第 2 步:我已经在该项目中添加了您的源文件,只是一个简单的示例适当的修改。我什using namespace std;至在头文件中放了那条邪恶的线。

实用程序.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

实用程序.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. 链接器->常规->附加库目录:$(OutDir)
  2. 链接器->输入->其他依赖项:XmasLib.lib
  3. 解决方案属性->ProjectDependencies:App 依赖于 lib

第 5 步:构建解决方案并运行应用程序。输出:Util::print() works! yay!

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

圣诞快乐!:D


PS:值得一看的问题:
为什么“使用命名空间标准”被认为是不好的做法?
什么需要我声明“使用命名空间 std;”?
在哪里放置 using namespace std;

于 2012-12-21T15:33:29.697 回答
0

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

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

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

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

于 2012-12-25T23:01:45.393 回答