C++ :: Including Header Files To Source File?

Jul 10, 2013

For example, I have the below files in a project called Calculate :

Source files : Calculate.cpp , Average.cpp (with out main)
Header files : Calculate.h , Average.h

I knew in general, we have to include Average.h to project header file Calculate.h to make it as part of project.

technical difference between adding header file (Average.h) to either project header file (Calculate.h) or project source file (Calculate.cpp) ?

I found no difference in an output. But, there must be technical difference.

View 3 Replies


ADVERTISEMENT

Visual C++ :: Should Precompiled Header Files Also Be Included In Source Header Files?

Sep 16, 2013

When including a header file in stdafx.h, should that file still be included in the source file where it is actually used?

If it is included in both places, is the one in the source file ignored?

View 5 Replies View Related

C/C++ :: Including Main Header File Which Includes The Including Head

Mar 28, 2015

I have a main.h file where I include all the needed things to make my program compile properly, string, vector etc.

And I also have another header file which comntains a class that is used later in the code (globally), I decided to keep it in another file to make it more clear and easier.

I need to include that file in main.h, but I also include main.h from that class header file because it contains some other includes that are required to compile.

Is this a good thing? Or should I keep main.h out of that class header file and include just things required for the class?

I may have complicated it too much, so I'll show an example, what I do now:

// ---- main.h ----
#include <string>
#include <vector>
#include <ctime>
// other includes, these are just examples
#include "MyClass.h" // the separated class header file

[Code] ......

So, from what you can see MyClass.h requires just including the vector, but to avoid repeating myself I include main.h which does that already, but also includes MyClass.h

So, I have two questions:
1. Is it ok to include in that way (including a file that includes the including file)
2. Is it good to include a main header file with all the includes even if I just need one of them, or should I skip including main.h and include just the things my class requires (vector is just an example)

View 2 Replies View Related

C++ :: Including Header Files Located In Two Different Directories

Oct 23, 2013

I have these two files:

/project/protocol/guarddog/report.h

/project/protocol/sky/report.h

I have two report.h files located in two different directories. However the contents of them are different. How can I include the report.h file located in guarddog into the report.h file located in sky?

I assume just doing this would be ambiguous:

#include "report.h"

View 1 Replies View Related

C :: Including Source File Twice But With Include Guard

Jun 14, 2014

I have four source files. The main source file includes two other source files. The two other source files both include the fourth source file. In the fourth source file I have an include guard. Will the code from the fourth source file exist in two locations in the compiled code? Is this something that is compiler dependent? An example of this is shown in the code below.

Code:

// filename: main.c
#include "source1.c"
#include "source2.c"

[Code]....

View 4 Replies View Related

Visual C++ :: Including New Header File

Apr 22, 2013

I have the header (and accompanying lib file) in my project folder, I have it in my solution explorer. And I've tried to add it via C++ Directories.. but that doesn't seem to exist anymore, instead it points to a user property sheet, but where to find or access it ...

View 3 Replies View Related

C++ :: SIP Parser And Header File Including (using Sofia-sip)

Nov 6, 2012

I am currently developing a small quick'n dirty SIP (Session Initiation Protocol) parser. Its task is to read a file (or command-line argument) with a SIP requst and parse it using the sofia-sip library. It should exit either with "1" on parsing failure or "0" on parsing success.

I have taken the code snipplet from here: [URL] .... resulting in this source code:

Code:
#include <iostream>
#include <stdio.h>
#include <sofia-sip/msg.h>
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mclass.h>
int main() {
char *msg_data = "...";

[Code] ....

When I try to compile the code using

Code:
g++ -o parser -I/usr/include/sofia-sip-1.12 parser.cpp

I get the following failure:

Code:
parser.cpp:10:45: error: "sip_default_mclass" was not declared in this scope ....

View 4 Replies View Related

C/C++ :: Placing Struct Of Object In Header And Source File?

Jul 19, 2014

Let's say I am using a library containing classes called class1 and class2 but both classes take three arguments to construct them. eg. class1(int a, int b, int c). and the same for class2

The below is an example of how to lay out the structure in the header and source file if class1 and class2 don't have any arguments in their constructor. But.... I'm not sure how to go about the below to take into account the constructor arguments of class1 and class2.

program.h
struct thestructure
{
thestructure(class1, class2); //constructor.

[Code].....

View 5 Replies View Related

C :: How To Share Variable Value From One Source File To Other Header File

Dec 11, 2014

I wanted to share the value of a variable from Sender Program to Receive after program and want to calculate difference between send and receive. After studying Header file concept I program following three.

Now I am struck. How to to compile? I link all these file. I used following method:

Code:
gcc Sender.c -o Sender Sender.h
gcc Receiver.c -o Receiver Student.h

Then I run Sender and after that Receiver.I per my knowledge, Receiver should give difference but it gives error :

Code:
Receiver.c: In function "main":
Receiver.c:10:42: error: "Send" undeclared (first use in this function)
printf(" Total Receive is %d
",Receive-Send);

Code:
Sender.c
#include <stdio.h>
int Send ;
void main(){

[Code] ....

View 2 Replies View Related

C++ :: Source File And Header File Are Not Compiled Together

Jan 30, 2013

My socket.cpp program got error. it showed "socket.h: no such file or directory". I had put my header file (socket.h) in the same place with my source file.

View 1 Replies View Related

C++ :: Including Header And Inheritance - Parent And Child Classes

Jan 27, 2012

I have defined to classes : Parent and Child. I have some global variables in a header file named as "var.h". These variables are used in both Parent and child Classes. The source code of these classes are written below:

Parent.h
==============================================
#ifndef PARENT_H
#define PARENT_H
#pragma once
#include <stdio.h>
class Parent {

[Code] ....

After compiling, the compiler returns a fatal error as follows:

1>Parent.obj : error LNK2005: "int counter" (?counter@@3HA) already defined in Child.obj
1>C:Documents and SettingspishiDesktop estDebug est.exe : fatal error LNK1169: one or more multiply defined symbols found

It says the "counter" is defined multiple times....

View 4 Replies View Related

C++ :: Using A Header File Across Multiple Files?

Aug 2, 2014

So say I create a header file which contains a list of structs, and I want to use these structs through out my source and some of my classes... how would I accomplish this?

When I try to do it via #include, I get re-definition errors, due to the nature of #pragma once. If I switch to #ifndef then I lack defenitions in files other than the source.

Is there a way to define things such as structs across multiple files, which doesn't lead to re-definition errors, and doesn't involve manually re-created all the structs for each file?

View 2 Replies View Related

C/C++ :: How To Compile Two CPP Files With Header File

Jan 28, 2015

this is my main cpp file:

#include<iostream>
#include<conio.h>
#include "C:UsersAmitDesktopNew foldersum.h"
using namespace std;
int main() {
int a;
int b;

[Code]...

this is my 2nd cpp file add two number

#include<iostream>
#include "C:UsersAmitDesktopNew foldersum.h"
int sum(int x,int y) {
return(x+y);
}

this is my header file used in both cpp file

#ifndef SUM_H
#def SUM_H
int sum(int x,int y);
#endif

error i am getting in main cpp in devc++

[Linker error] undefined reference to `sum(int, int)'
[Linker error] undefined reference to `sum(int, int)'

in 2nd cpp file [Linker error] undefined reference to `WinMain@16' [/code]

View 1 Replies View Related

C/C++ :: How To Get Multiple Source Files To Run One After The Other

May 16, 2012

I am trying to run multiple source files but right after the first one finishes running the program closes and doesn't move on ...

View 2 Replies View Related

C :: Accessing Pointers From Other Source Files

Oct 16, 2013

I've recently been learning GTK (though this question is not specific to GTK), and came across a situation that I was unsure how to best handle. Essentially, I've defined several pointers in one source file, and I want to access those pointers from other source files.

The structure of my GTK programs generally follow this pattern:

- "main.c": Define the main window and run GTK main
- "create_window.c": Create and arrange widget pointers in the main window
- "program_functions.c": All other source code for the project (several source files in reality)

In "create_window.c", I declare and define all my widget pointers (e.g. label). If I need to modify those widgets in "program_functions.c" for any reason (say, to change the value of a label), I need access to the pointers created in "create_window.c".

My first thought was to create a global struct of pointers in "create_window.c", and extern that struct to the other source files that need access to the pointers. The thing I don't like about this approach is spreading globals across my program.

My second idea was to create access functions in "create_window.c" where the necessary pointers are statically stored. The first time I call this function (immediately after creating a widget), a static copy of that pointer is stored in the function. Each time afterwards when I call that function (from other source files), I simply use that static pointer to access the widget of interest.

Example:
Code: void edit_label_1(GtkWidget *label_set, const char *string)
{
static GtkWidget *label = NULL;

[Code].....

Are either one of the approaches considered acceptable by standard practice?

View 2 Replies View Related

C :: Compile Multiple Source Files

Mar 7, 2013

How I can compile multiple source file in visual studio 2012 ???

View 1 Replies View Related

C :: How To Share Imported DLL Functions Between Source Files

Nov 24, 2013

In general, my problem is that I've been trying to reorganize the project I and my group are working on into separate project files. Everything was working perfectly fine before, but now I'm facing the wrath of undefined reference errors when I try to call my DLL functions.

I have them declared and included in the central header file here :

Code:

/* DLL functions */
typedef void ( * t_wait )( int milliseconds, const int frames_per_seconds );
typedef clock_t ( * ft_timer )( int command, t_timer * timer_object ); /* clock_t is usually defined as long */
typedef void ( * t_SDL_errorexit )( const char * message );
typedef SDL_Surface * ( * t_load_image )( char * image_path, unsigned is_transparent );

[Code]...

Then I have them imported here :

Code:
/* ******************************************************** */
/* ********* win_error( char *, bool ) ********* */
/* ******************************************************** */
/* - Displays a GUI for a windows specific error message, */
/* pass true to it to have it exit the program, pass false */
/* to have it continue */
/* ******************************************************** */
extern void win_error( char * message, bool is_exit )
{
/* Note : win_error uses the Win32 Api */
/* ********************************** */
char buffer[BUFSIZ] = { 0 }; /*

[code]...

This doesn't work, as my compiler sees it that I am trying to call invalid functions, whereas I have made sure to import the functions before calling. Obviously my compiler can't tell that, and is trying to protect me from calling them.

The source files are compiled as C++, but I'm using C-style code instead of true C++ code.

Obviously I'm using Windows style DLL linking, but if the only ways to share imported functions are non-standard, only post ways that will work on as low as Windows XP.

I have tried to statically link them before, but that led to a problem that I posted a while back. Which led me to dynamically linking, which was problem-free until I separated my code into separate files.

View 13 Replies View Related

C++ :: Program Needs To Compile Various Source Files At Runtime

Oct 30, 2014

My program needs to compile various source files at runtime.What is the most elegant way to compile cross platform with g++ from within my program? Is there a gcc-library I can use? I know that I could use popen() to open a Unix pipe and call g++ as command line tool. But first it isn't really cross platform and second it doesn't seem elegant to me.

View 10 Replies View Related

C++ :: Reading Source Code From Uncompleted Files?

Jun 9, 2013

I know it sounds strange but I've seen things that have files which contain source code (usually in something in Python or such) and how this is read on run-time?

View 1 Replies View Related

C++ :: Source Files Separation And Inter-related Classes

Feb 11, 2012

I have heard that people should implement their class member functions in files that are different from their class declaration files. But in cases there are multiple classes that are inter-related to each other, how will you review the source code ?

View 1 Replies View Related

Managed C++ And C++/CLI :: Breaking Up Large Class Into Manageable Source Files?

Feb 9, 2015

I'm trying to make sure my code is written in smaller modules, so my first step is to create my initialization process in and external file to load the necessary data from external sources and set up things like the content of drop down list boxes.

My first attempt failed to give me access to the combobox items add function so I moved that code back into the form1.h file:

Code:
public:
Form1(void) {
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void AddDate(char *date, int ID)
{
this->comboBox1->Items->Add("line 1");
}

It compiles fine, but the call to it in my Initialize.cpp file

Code:
MarketView::Form1::AddDate("abs",1);
Gives error C2352: 'MarketView::Form1::AddDate' : illegal call of non-static member function

OK, so I change "void AddDate" to "static void AddDate" and now get the error that "static member functions do not have 'this' pointers" so I go back to the "MarketView::Form1::comboBox1" situation where there is no legal syntax after "Box1 to get me to Items->Add

I've been an old fashion programmer for over 47 years. It seems as is the concept of programming computers has changed from the concepts of logic to memorization of complex syntax.

There has to be a simple answer to do this other than to write thousands of lines of code in one Form1.h file. I refuse to believe that the new programming concepts will not allow you to write code in smaller more manageable modules.

What is the proper syntax for breaking up the larger file into more manageable chucks?

View 12 Replies View Related

C/C++ :: Getting Header Error C2447 / Can't Find Error Source

Sep 8, 2014

Cannot manage to find the error source when i try running the program, the first part of the program runs just fine its when i try to get the temperature one that i get the error

#include <iostream>
#define pi 3.141592
using namespace std;
int main() {
double r, h; //declare variables for radious and height
double Surfacearea;

[code]....

View 1 Replies View Related

C :: Header Files Not Found On Mac

Nov 2, 2013

I have a piece of code in C with header files included. I run it on Mac OS X Maverick with XCode 4.6.2 installed. GCC is also installed. Note that Command Line Tools in XCode are already installed.

When I compile it, the error I receive says something like this:

add.c:1:19: error: stdio.h: No such file or directory add.c:2:20: error: stdlib.h: No such file or directory add.c:3:20: error: unistd.h: No such file or directory

However when I run it on Ubuntu, it compiles without a problem.What to do?

View 2 Replies View Related

C++ :: Using Namespaces In Header Files

Dec 9, 2013

So I've been making a header file and put variables in their own namespace to avoid conflicts. My question is, do functions in the header file normally go in a namespace too, or should they just be named in a way which makes them unlikely to be accidentally copied?

View 15 Replies View Related

C++ :: Precompiled Header Files

Apr 2, 2013

My teacher talks about header files just having definitions and not declerations. I am writing a program that has a .h file and a related .cpp file along with a main.cpp it would be nice to have the .cpp file associated with the .h file compiled into an object file that would than just be referenced when the .h file is included. Am I making any sense?

View 2 Replies View Related

C++ :: How To Use Two Header Files In Program

Sep 17, 2013

I want to use two header files in my program. Here is exactly what I want to do.

-In the first header I have a binary tree and a structure.
-In the second file I have another functions that need to use the structure in the first header.
-I also want to use a function from the second header in the first.
-And finally I want to do actions with both headers in a "main.cpp" file that contains only int main() function.

How to include the headers in each other and in the main.cpp to be able to do the actions above?

I try to include the first header in the second one and the second one in the first header. Then I include both headers in the main.cpp file. But the compiler shows me many errors.

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved