Making header with VS

Mar 22, 2015 at 8:33am
Hello. I have some problems making header file with Visual Studio 2013. I do exactly the same steps from this ( https://msdn.microsoft.com/en-us/library/ms235627.aspx ) guide.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Source.cpp
#include"Header.h"
#include<string>
#include<stdexcept>
using namespace std;
string to_bin(int num)
{
	string mystring;
	do
	{
		mystring += to_string(num % 2);
		num /= 2;
	} while (num != 0);
	reverse(mystring.begin(), mystring.end());
	return mystring;
}


1
2
3
4
5
//Header.h
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
string to_bin(int num);
#endif 

It shows some errors like

error C2146: syntax error : missing ';' before identifier 'mystring'

I'm sure the function works fine with these libraries.
Also, I tried the same code from MSDN and still won't build.
Last edited on Mar 22, 2015 at 8:37am
Mar 22, 2015 at 9:33am
1
2
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED 


Change that to

1
2
#ifndef HEADER_H
#define HEADER_H 
Mar 22, 2015 at 9:37am
Still the same.
Mar 22, 2015 at 9:40am
No idea how I didint see this earlier. You have to include string in your h file aswell.

#include <string>

otherwise you cant do this -

string to_bin(int num);

Also. String is part of the standard library. So either you have to do using namespace std; in your header file. Or do std::string to_bin(int num)
Last edited on Mar 22, 2015 at 9:45am
Mar 22, 2015 at 10:31am
Actually, this almost worked. So, I need to include the same libraries in both files?
Also, why I had to change this?

#ifndef HEADER_H
#define HEADER_H




Now I get LNK1120 error when I try to use it in another program.
I think I need to do this:
When you build on the Visual Studio command line, you must build the program in two steps. First, run cl /c /EHsc MathFuncsLib.cpp to compile the code and create an object file that's named MathFuncsLib.obj. (The cl command invokes the compiler, Cl.exe, and the /c option specifies compile without linking. For more information, see /c (Compile Without Linking).) Second, run lib MathFuncsLib.obj to link the code and create the static library MathFuncsLib.lib. (The lib command invokes the Library Manager, Lib.exe. For more information, see LIB Reference.)

But I'm not sure how.
Last edited on Mar 22, 2015 at 10:33am
Mar 22, 2015 at 11:49am
Now I get LNK1120 error when I try to use it in another program.

my guess is you didn't do this

2.Before you can use the math routines in the static library, you must reference it. To do this, open the shortcut menu for the MyExecRefsLib project in Solution Explorer, and then choose References. In the MyExecRefsLib Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.


3.The Add Reference dialog box lists the libraries that you can reference. The Projects tab lists the projects in the current solution and any libraries that they contain. On the Projects tab, select the MathFuncsLib check box, and then choose the OK button.







I think I need to do this:

When you build on the Visual Studio command line, you must build the program in two steps. First, run cl /c /EHsc MathFuncsLib.cpp to compile the code and create an object file that's named MathFuncsLib.obj. (The cl command invokes the compiler, Cl.exe, and the /c option specifies compile without linking. For more information, see /c (Compile Without Linking).) Second, run lib MathFuncsLib.obj to link the code and create the static library MathFuncsLib.lib. (The lib command invokes the Library Manager, Lib.exe. For more information, see LIB Reference.)


no you don't need to do this
Last edited on Mar 22, 2015 at 11:50am
Mar 22, 2015 at 3:07pm
I'm a bit confused. I don't have "References".
Apr 5, 2015 at 4:16pm
Just did it with Code::Blocks using this: https://www.youtube.com/watch?v=TWVNUA4NO9U

Kinda easier.
Topic archived. No new replies allowed.