0

I'm trying to use the static library however when I add the reference and include directories to the project I get strange compiler errors, this including only any header file.

The static library project builds successful. I don't know where the error may be, but I suppose it may be in some file of the static library.

The static library files are the following:

header:

#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
}

}

the file that tries to use the static library:

#include <iostream>

using namespace std;
//using namespace Kaczmarz;

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

Note that I'm not calling yet any function from the library.

The errors that I got are the following:

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

Note that the errors mention the std string class.

RandomGuy
  • 648
  • 6
  • 21
  • 5
    Note, that you wrote `using namespace std;` at the beginning of the header that will be included in every project that will use your library... – LihO Dec 21 '12 at 14:59
  • 1
    **never** put `using namespace std;` in a shared header file (to expand on LihO's comment, in case you're wondering why its getting up votes). – WhozCraig Dec 21 '12 at 15:02
  • 2
    Expanding on what LihO wrote: I would remove the 'using' from the header file and use explicit namespace on the types (i.e std::vector) – Mustafa Ozturk Dec 21 '12 at 15:02
  • 2
    It looks like the code you're compiling is missing a `;` somewhere. I don't see how you could get those errors from the code you posted though, unless one of the system headers is broken. – Mike Seymour Dec 21 '12 at 15:04
  • @MikeSeymour there's a lot of ... in his post. So there's a lot of code missing, he is missing a ; somewhere though, I concur. – johnathan Dec 21 '12 at 15:08
  • I've just updated all the code. Now without the "...". @LihO I've used explicit the namespace on types too. But getting the same compiler errors. – RandomGuy Dec 21 '12 at 15:20
  • @WhozCraig: All member functions are _within_ namespace enclosing. – Sebastian Mach Dec 21 '12 at 15:26
  • @phresnel I really need to get a taller monitor. I found the closing curly. thanks. – WhozCraig Dec 21 '12 at 15:28
  • @RandomGuy with the updated source, how exactly is that source file trying to "use the static library" ? It doesn't include the static libs headers, and doesn't use any of the static libs code.? Is something still missing? Also, is it that *specific* source file (main.cpp) that is puking up all these errors on compilation, or is it another? – WhozCraig Dec 21 '12 at 15:43
  • @WhozCraig My idea was to call some static functions from the static library. Yes it's not using any of the functions yet but it doesn't compile when I do the linkage. Probably I'm doing it wrong. It's the main.cpp file which is triggering this errors after the linkage. – RandomGuy Dec 21 '12 at 15:59
  • @RandomGuy the errors you still have posted in this question are compile-time, not link-time errors. If your updated source now has different errors please *amend* the question to show what they are. – WhozCraig Dec 21 '12 at 16:01
  • @WhozCraig Yes seems that I didn't explained well. The errors appear when I build the project, but only after I made the linkage. – RandomGuy Dec 21 '12 at 16:13

2 Answers2

4

Since it's Christmas time and I see that you're using MSVS, here are steps that I did to try your library:

Step 1: I have created new solution called XmasTest with new Win32 project in it called XmasLib.

Step 2: I have added your source files in that project, just a simple example-appropriate modification. I have even let that evil using namespace std; line in the header file.

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 */

Step 3: I have created new Win32 Console application called XmasLibTestApp with following code in it:

#include <iostream>

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

using namespace std;
using namespace Kaczmarz;

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

Step 4: Since these are 2 projects within 1 solution, I have handled the dependency in the following way:

  1. Linker->General->Additional Library Directories: $(OutDir)
  2. Linker->Input->Additional Dependencies: XmasLib.lib
  3. Solution Properties->ProjectDependencies: App depends on lib

Step 5: Build Solution and run app. Output: Util::print() works! yay!

And that's it. End of the story, everything works, developer rejoices with his IDE.

Happy Xmas! :D


PS: Questions that are worth to have a look at:
Why is "using namespace std" considered bad practice?
What requires me to declare "using namespace std;"?
where to put using namespace std;

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • Hi thanks for your answer (I have gave it +1). Unfortunately it doesn't solve my problem. I will post a own answer how I solved this problem so people with the same problem can know the source of it. Thanks again for your time. – RandomGuy Dec 25 '12 at 22:56
0

Ok found the cause of this compiling errors.

In the "additional include directories" (vs 2012) I had another .cpp/.h files which imported "Util" class.

I just removed this files from the directory and this time it compiled good without any errors. Note that this files weren't present in the project (I had excluded them), however seems that "additional include directories" linked them somehow in the project.

Thanks to all who have answered, I really appreciate it.

RandomGuy
  • 648
  • 6
  • 21