Place function in header file?

Hello, again.
Suppose I have a piece of code that I know I am going to be using very often. Obviously, the logical thing to do is place it in a function. And, suppose that I know I'm going to be calling that function often, and in a lot of different projects. How can I best make it so that I don't have to declare and define the function in every file in every project that I want to use this function in?
Would I place the function in a header file, and then use the #include preprocessor directive to include the .h file in every file I want to use the function in? If so, why couldn't I just store the function in a .c/.cpp file? What does a header file offer over a .c or .cpp file -- why use one over the other?
Also, where do I go to create the header file? If I go to File > New File and select header file, then it creates it as a file within the project that I have open at that moment, and I'm going to want to use the file in other projects. Is there somewhere I can save the header file outside of any specific project, so that I can include it in any project I want?
Thanks.

Tron55555 wrote:
So, technically, I could define the entire function in the header file, even without declaring it as inline, and I wouldn't have to define it in a source file?
Nope. You'll get linker errors, or at least warnings, about the function being defined twice.
1.) Why define it in a source file in the first place? Why not always define it in the header file? I can imagine there's obviously a very good reason for this -- I'm just curious what it is.
Code bloat and the sheer inefficiency of having the same code duplicated everywhere.
2.) You said that if the function is defined in the header file instead of a source file then it is treated as a macro and the whole function is picked up and dropped in your code wherever it is called. This sounds like the same thing as an inline function, so if you did define the entire function in the header file, what would be the difference between defining the whole thing in the header file using the inline keyword, versus defining the whole thing in the header file without the inline keyword?
Well, it is an inline function. You must use the inline keyword.
3.) Finally, if I did decide to define the entire function in the header file and not use a source file, would I still need to write the function signature as well, or just the definition?
Just the definition.

Similar Messages

  • File:Place Function Bug in Snow Leopard (CS4)

    Hi all. Have any of you experienced this bug when running Mac OS 10.6.7 (Snow Leopard)...the File:Place function no longer works for text.  I can place graphics/photos, but when I try to place text nothing happens.  Any thoughts about this?  I'm using InDesign CS4.
    Thanks!

    I'm a Windows guy, so I'm shooting in the dark at this point. Happens in all files? How about in a new user account?
    What has changed recently ont he system? New fonts? Any software updates?
    Do you run a font manager?

  • Header File Functions Not Recognized

    hi, this is my first discussion on this form.
    i am trying to make a custom calculator using C++.i am trying to use a function that i have included in a header file, but it fails to compile and says i'm using an using an undeclaired identifier.
    i have 4 header files and only one of them (squared.h) actually lets me access my functions inside. i am new to C++.
    i am running OSX Lion and using Xcode 4 for my compiler.
    Main.cpp
    #include <iostream>
    #include "squared.h"
    #include "factorial.h"
    #include "squareroot.h"
    #include "pythagoream.h"
    int main ()
        squareroot();
    pythagoream.h
    #ifndef iostream
    #define iostream
    #ifndef cmath
    #define cmath
    using namespace std;
    double pythagoream()
        cout << "Is it a Side or Hypotonuse(s,h): ";
        char a;
        cin >> a;
        if (a == 's')
            cout << "Enter Hypotonuse Length: ";
            double b;
            cin >> b;
            cout << "Enter Known Side Length: ";
            double c;
            cin >> c;
            double d = sqrt((b * b) - (c * c));
            cout << "The Unknown Side Length Is: " << d << endl;
        if ( a == 'h')
            cout << "Enter Length Of Side 1: ";
            double b;
            cin >> b;
            cout << "Enter Length Of Side 2: ";
            double c;
            cin >> c;
            double d = sqrt((b * b) + (c * c));
    #endif
    #endif
    squared.h
    #ifndef iostream
    #define iostream
    using namespace std;
    void squared()
      cout << "Enter Any Number: ";
      double x;
              cin >> x;
              cout << x << " squared is " << x * x << endl;
    #endif
    squareroot.h
    #ifndef iostream
    #define iostream
    using namespace std;
    void squared()
      cout << "Enter Any Number: ";
      double x;
              cin >> x;
              cout << x << " squared is " << x * x << endl;
    #endif
    factorial.h
    #ifndef iostream
    #define iostream
    using namespace std;
    int factorial()
    int n;
    int factorial,i;
    cout << "Enter a positive integer: ";
    cin >> n;
              for (i = 0; i <= n; i++)
                        if (i == 0)
                        factorial = 1;
      else
                        factorial = factorial * i;
              cout << "The factorial of " << n << " is " << factorial << endl;
    #endif

    You need to include iostream in the header files where it is used and not in Main.cpp

  • Shared library: function is not found and recognized in header file

    Hello,
    I am trying to use Java methods into LV. I am doing so by creating Java Invocation Interface, usind which I can call Java methods into C++ and then create a shared library that can be called into LV.
    When I am importing my shared library into Labview, I am getting the following error messages:
    The shared library contains 3 function(s). But no function is found and recognized in the header file. The following function(s) cannot be wrapped. If you want to import these functions, please review the warning messages next to the functions below. You will need to fix the problems before you can continue with the wizard.
    jclass invokeJavaClass(JNIEnv* jenv, string className);
    The following symbols are not defined:
    jclass;
    Undefined symbols can prevent the wizard from recognizing functions and parameters. To correct this problem, check the header file to determine if you must add preprocessor definitions. Click the Back button to return to the previous page of the wizard to add a preprocessor definitionsl (for example, "NIAPI_stdcall = __stdcall" or "NIAPIDefined = 1").
    The following header file was not found in the specified header file or one of the referenced header files:
    -  string
    -  iostream
    -  cstring
    -  jni.h
    To fix, click the Back button to go to the previous page and add the header file path to the Include Paths list.
     Please advise.
    Regards,
    H
    Attachments:
    SharedLibError.png ‏51 KB

    Hello Vivek,
    The LabVIEW dll that I am trying to import does not include any third-party device..all my code is fully based on LabVIEW. Maybe this helps you to guess what is happening: once I've parsed the dll' header appears an error
    like this one:
    void
    __cdecl Zdmt(LVBoolean *stop, double P, char channelName[],
        TD1
    *errorIn, TD14 *FFTOptions, TD12 *Calibration, char FileName[],
    int32_t minRecordLength, TD26 *InstrumentHandler, LVRefNum
    sessionRefArray[],
        LVRefNum *queueIN, TD1 *errorOut, LVBoolean
    *averagingDone,
        HWAVES LastRecordFetched, TD24 *Impedance, TD17
    *ColeColeCluster,
        TD18 *FFTcluster, TD5
    *InstrumentHandleOutputCluster, LVRefNum *queueOut,
        int32_t
    *Acquired, TD6 *FreqTimeInfoCluster, double *averagesCompleted,
    int32_t len);
    The following symbols are not defined:
    LVBoolean;
    int32_t; LVRefNum;
    Undefined symbols can prevent the wizard
    from recognizing functions and parameters. To correct this problem,
    check the header file to determine if you must add predefined symbols.
    Click the Back button to return to the previous page of the wizard to
    add a preprocessor definitionsl (for example, "NIAPI_stdcall =
    __stdcall" or "NIAPIDefined = 1").
    The following header file was
    not found in the specified header file or one of the referenced header
    files:
    -  extcode.h
    To fix, click the Back button to go to the
    previous page and add the header file path to the Include Paths list.
    I have replaced the first line #include "extcode.h" of
    the dll header file for #include "C:\Program Files\National
    Instruments\LabVIEW 8.6\cintools\extcode.h" that is the full path where
    the header file is located. However, new libraries seems to be missed:
    -  stdint.h
    -  MacTypes.h
    As far as I know,  Mactypes.h contains basic mac os data types and it doesn't have any relation with stdint.h...
    I have created both of them and stored into the same folder as extcode.h, but then other libraries are missed!!!
    Do you know if it would be possible to create the .dll generating all the header files associated for its data structures???
    And if this is not factible, then what do you suggest me? because I hope to not having to create all the header files until it stops giving me an error!
    thanks for four time,
    ben

  • Concerning header files and include files

    I presently have a site I created in Frontpage 2003, Gunshows-USA.com.  It has seperate pages for each state and month.  I had a header file called head.shmtl that I included on each page.  This allowed me to create one file and reuse it on 50 different pages.  I was also able to place it on the page in such a way that I could include it under the Title of the page and even iinsert an indiviual banner above the header.shtml.  Will I be able to do basically  the same thing with Dreamwaver 5.5s header and footer function?  If so, is there anything special I need to be aware of?

    I guess I didn't quite explain myself as well as I should have. 
    1st I had used include files on my site for a header and a footer but the site broke and when I got it back up it was necessary to load code for each header file to each page to get it to appear.  I am not presently using include files on the site.  The site was created in FrontPage 2003 and I intend on migrating it to Dreamweaver as soon as I know a little more about what I'm doing with DW
    2nd.  When I had the include files working I did NOT have to change everything on every page when I wanted to change something in the header or footer.  The way I have it set up now I do.
    3rd.  Going through different tutorials books, etc I see that I can create a file with a header, a body, and a footer from the templates that are built into the program.  An example is the 3 column fixed, header and footer. My question is, if I use this layout will that layout follow throughout the site and will I be able to basically do the same as I did when I was using includes?  Will a change to the header file change every header file?
    4th.  Can I position the header file sot that it has space above it.  Or another words can I have the header file appear in the middle of the page?
    5th.  Is if possible to have more than one header or footer file and be able to call the one I want or use all one after another?

  • How can I use a dll if I dont have a header file

    I'm not sure if I'm even trying the possible here as I have searched and not been able to find much at all.  However I figured it was worth asking here.
    I have access to several dll's used by a program, I need to open a file using the program (for some reason it is completely non responsive unless you open it "within" the program itself) and so decided to browse the .dll files included.  Ive found a few functions which may carry out the function I need.  Is there a way of figuring out the inputs/outputs if I don't have documentation or a header file?
    This is the next stage in a huge project I am working on at the moment and I've been banging my head against the wall all day trying to figure this out.
    Thanks in advance for any help
    Rik
    That glass?
    Thats glass is neither half full or half empty....
    Its twice the size it needs to be

    Yes, that makes sense. It also means that what you are trying to do is not likely to work. You have no way of knowing what the program does when opening the file, so guessing at using the DLLs is purely a shot in the dark without even knowing where the dark is. Even if you could find the function (assuming it's just one) that loads a file, how is the program supposed to use it now? That function has to be called from within the program. When you call it from LabVIEW you are not sitting inside the program's memory space, so it has no way of knowing about the file.
    I would suggest, instead, to see if the program accepts command-line parameters. For example, does it accept a name of a file to open as part of launching it from the command line? If not, then you may need to resort to trying to control it via automation. If it has no built-in automation then you need to resort to using the OS to make pretend you're clicking buttons and typing text. This has come up many times before, and there have been numerous posts on this, so please do a search on controlling an external program from LabVIEW within this forum. You can call the Windows API functions to move the mouse to a specific location and click the button as well as typing text, or you can use third-part automation tools. One that I have used successfully is AutoIt. The search I indicated will yield other suggestions. 

  • How to use preprocess​or directives (#define) in C++ header file with LabVIEW 8.2

    I have a C++ header file that contains around 2000 preprocessor directives:
    #define MEM_1   0xC
    #define MEM_2   0xD
    #define MEM_3   0x18
    I want to be able to "access" these memory offsets by identifier name (MEM_1) in my LabVIEW program like I would in a C++ program.  I do not want the overhead of parsing through the header file and storing all the offsets into an array or similar structure. 
    I've written a simple Win32 console program to return the memory offset given the identifier (see code below), and created a DLL to use with my LabVIEW program.  In the console program, you notice that I can call a function and pass in the identifer name, and get the offset back correctly:
    getOffset(MEM_1);
    In LabVIEW, I was hoping to be able to pass in the identifier (MEM_1) but was unsure what datatype to use.  In my C++ code, I defined the parameter as an int.  But in LabVIEW, I can't enter in MEM_1 as an int.   Can someone advise on how to do this?  Or if there is an alternate way to use #define's from external code inside LabVIEW?
    #include "stdafx.h"
    #include "scrmem.h"
    #include "stdio.h"
    void getOffset (int var);
    int _tmain(int argc, _TCHAR* argv[])
     getOffset(MEM_1);
    canf("%d");
     return 0;
    void getOffset (int var)
     printf("The address of MEM_1 is %x", var); 

    kaycea114 wrote:
    Hi,
    Where do you think I should use the string? 
    The way that getOffset is currently defined in the DLL, I have to connect an integer input into the LabVIEW function.  This prevents me from entering in: MEM_1 as the input to the LabVIEW function.
    Are you suggesting that I change getOffset to receive a String parameter ("MEM_1")?  Does that mean I need to do a string compare (line by line) through the header file to get the offset?  It seems like doing this search through the header file would degrade performance, but if that's the only work around, then I'll do it.
    Please advise.
    Well, what you want to do is indeed entering a string and getting back the assigned integer. That is what the C preprocessor is doing too although there it is done only once at the preprocessor stage of course and not at runtime anymore. But LabVIEW is not a C preprocessor.
    What you did so far seems to be to define getOffset() that accepts an enum that needs to be created from the C source code to then return the assigned constant. That's of course not very helpful.
    And writing a VI that could parse the C header file and create a name/constant array is really a lot easier than doing the same in C. You don't even need to parse the file each time again, but can instead cache them in an uninitialized shift register (LV2 style global).
    Even more easy would be to create from that data a ring control using property nodes and save it as custom control and voila you have the most direct lookup you can get in LabVIEW and it works just as comfortable as using the define in C code. It would mean that you need to seperate your header file possibly into several different files to only get related constants into the same ring control, but that is easily done.
    Rolf Kalbermatter
    Rolf Kalbermatter
    CIT Engineering Netherlands
    a division of Test & Measurement Solutions

  • Create header file based on data in Data file

    Hi,
    For Data File: created an interface to extract data from table to data file.
    1) Now i want to create a header file which contains info like :
    Created On: #Rows Fetched:
    2) I also want to create a footer, which contains info like:
    SUM(AMOUNT) (amount is one of the columns of data file)
    How do i implement this using an interface?
    Thanks.

    Step 1. Create a Variable Header - Call the count using some thing like this SELECT COUNT(*)||CHR(10) FROM TABLE
    Step 2. Call it in OdiOutFile
    Step 3 . Call the Interface to load from table to File using [ IKM SQL to File Append  with create Header option - no ]
    Step 4. Create another Variable Footer - Call the sum in the Refreshing Tab
    Step 5. Call it in OdiOutfile with Append option Checked
    Note - Make sure you use the same File Name for all the three places.
    There are other ways too but this is simple way. Hope this helps.

  • Acrobat Pro "save as" function in the file menu does not show the current directory of the file

    I have installed CC but Acrobat Pro gives me headaches.
    The "save as" function in the file menu does not show the current directory of the file but something I have worked on in the past.
    It forces me to navigates through my hard disk for this very straightforward thing in normal conditions. I precise: Acrobat is the only software reacting like that.
    I use Windows7 professional 64bits.

    I use what seems to be the latest version available on the Cloud:
    To illustrate my point, I open a directory with explorer and double-click on the only PDF in there:
    So here it is, open:
    When I do a "Save as", it takes me to a totally different place on the disk!
    Very annoying...
    Thanks for your time!
    JJ

  • Do I need to declare my custom setter accessor in my interface/header file

    Hi there:
    I'm working through a Objective C tutorial. I've got an instance var that is declared in the var section of my @interface (in the header file). It's also declared as a property...all the defaults apply (assign and whatever else).
    @property int foo
    I also have it synthesized in my @implementation code.
    @synthesize foo;
    I need a custom setter though that checks out the value being assigned. Do I need to declare this setter function in my @interface section in the header file?
    -(void)setFoo:(int)newValue {
    if (newValue < minAllowed) {
    NSLog(@"Invalid foo: %i is less than the minimum of %i allowed", newValue, minAllowed);
    return;
    if (newValue > maxAllowed) {
    NSLog(@"Invalid foo: %i is greater than the maximum of %i allowed", newValue, maxAllowed);
    return;
    foo = newValue;
    } // setFoo()

    The @property statement is the declaration of the accessor methods whether they are synthesized or custom coded.
    For a fairly clear, detailed explanation see Accessor Methods in the +Memory Management Programming Guide for Cocoa+.

  • How to use function in dll file

    hi
    i am new to jni. i want use the function declared in the dll function. i am also having the .h header file.
    can anybody please help me out with this?
    thank you
    suhas

    What kind of function? Function with name created
    with javah generated name or arbitrary function? In
    first case load library and just use "function()", in
    second case you should call something like "rundll"
    under Windows or create wrapper to call from Java and
    redirect call to DLL function itself.Thanks for the reply Michel,
    Actually I have connected RFID 6500 module to PC through com port. and want send request for which module gives response.
    The commands for the module are declared in FeCom.dll and also in FeCom.h. The definitions of the functions are given in FeComDef.h.
    Now I get problem while accessing function in these files.
    did you get the problem?
    waiting for reply
    Thank you,
    suhas

  • A question about /usr/include/sys/select.h header file

    good day! i hope i can get some help about this and i would be very grateful if my question will be answered. i have a question regarding the select.h header file that we include in our programs when we use the select(3C) function.
    * Select uses bit masks of file descriptors in longs.
    * These macros manipulate such bit fields.
    * FD_SETSIZE may be defined by the user, but the
    * default here should be >= NOFILE (param.h).
    29 #ifndef FD_SETSIZE
    30 #define FD_SETSIZE 1024
    31 #endif
    its says here in the header file that the value of FD_SETSIZE may be defined by the user and i need to know how. i've already tried declaring a constant at the beggining of the program but it didn't work...
    example:
    #undef FD_SETSIZE
    #define FD_SETSIZE 2048
    the FD_SET macro generates an error if a value larger than 1024 is passed from in the first argument. i need to increase its value in order for me to wait for more than 1024 socket descriptors on select function. how can i increase or modify its value? thanks in advance...

    AFAIK, the only way to change the FD_SETSIZE value is by building your own version of the libc.a and linking it statically to your executable. Not good at all IMHO.
    The sources of Solaris 8 contain the implementation of select(3C) with both standard and 'large' (64K) FD_SETSIZE.
    AFAIK, 64-bit version of the libc.so contains the 64K-descriptors version of the select(3C), so you may consider building your application with -xarch=v9 flag to get 64-bit application...
    As for poll(2) - there is a manual page (man -s 2 poll) and some notes in the Answerbook. poll(2) is the actual system call that is used by select(3C), which is an interface function, so there is no functional difference between these two.
    There is a potential problem with portability of your code if you are using poll(), but it looks like you have no choice... sorry about it...

  • A questions about /usr/include/sys/select.h header file

    good day! i hope i can get some help about this and i would be very grateful if my question will be answered. i have a question regarding the select.h header file that we include in our programs when we use the select(3C) function.
    * Select uses bit masks of file descriptors in longs.
    * These macros manipulate such bit fields.
    * FD_SETSIZE may be defined by the user, but the
    * default here should be >= NOFILE (param.h).
    29 #ifndef FD_SETSIZE
    30 #define FD_SETSIZE 1024
    31 #endif
    its says here in the header file that the value of FD_SETSIZE may be defined by the user and i need to know how. i've already tried declaring a constant at the beggining of the program but it didn't work...
    example:
    #undef FD_SETSIZE
    #define FD_SETSIZE 2048
    the FD_SET macro generates an error if a value larger than 1024 is passed from in the first argument. i need to increase its value in order for me to wait for more than 1024 socket descriptors on select function. how can i increase or modify its value? thanks in advance...

    AFAIK, the only way to change the FD_SETSIZE value is by building your own version of the libc.a and linking it statically to your executable. Not good at all IMHO.
    The sources of Solaris 8 contain the implementation of select(3C) with both standard and 'large' (64K) FD_SETSIZE.
    AFAIK, 64-bit version of the libc.so contains the 64K-descriptors version of the select(3C), so you may consider building your application with -xarch=v9 flag to get 64-bit application...
    As for poll(2) - there is a manual page (man -s 2 poll) and some notes in the Answerbook. poll(2) is the actual system call that is used by select(3C), which is an interface function, so there is no functional difference between these two.
    There is a potential problem with portability of your code if you are using poll(), but it looks like you have no choice... sorry about it...

  • Ther are PARAMETER LIST DISCREPANC​IES in the help file vs the NIDAQ.H header file for GPCTR_Read​_Buffer...

    PROBLEM: The function help file contains documentation for using GPCTR_Read_Buffer with 7 parmeters in the parameter list. The NADAQ.H Header file contains 8 parameters in the function declaration. Which is right? What is the correct parameter list and can an example be provided that shows how to use the function correctly? I am currently using DAQ ver 6.92 and a 6602 Counter Timer board in a buffered start/stop or ND_BUFFERED_TWO_SIGNAL_EDGE_SEPARATION_MSR application.

    Thanks for the response Filipe, I'll let you know how it goes. I only went into the header file because example code provided by NI ... paste ...
    iStatus = GPCTR_Read_Buffer(iDevice, ulGpctrNum, ND_READ_MARK, ulReadOffset, ulNumPtsToRead, ulTimeOut, &ulNumPtsRead, pulReadBuf);
    showed use of 8 parameters - in agreement with the header file and in disagreement with the on-line help file function documentation.
    Tom

  • How to call a function in DLL file, while writing test scripts in ATML standards

    Hello all,
                I am trying to develop test scripts in ATML standards. I need to call a function in DMM.dll file. How can i do that in ATML standards, please explain with some example ATML standard XML file. Someone please help me in doing so. Thanks a lot in advance.
    With Regards,
    kalyan

    Hi srivinayaka,
                         I am attaching the XML file i have written in ATML standards and the Labwindows/CVI code that was generated when the XML file is opened in TESTSAND. actually the defenition of the function "IALdcpwr_reset()" is in IALdcpwr.lib and IALdcpwr.h files.
    "How can we add this library and header files?" so that i can show the path of definition. whether it should be done manually or can we do some changes in the XML file and make the work done?. Thanks in advance. If you have any example for this type of sequences. please forward me which would be helpful for me.
    With best regards,
    kalyan
    Attachments:
    XMLfile.doc ‏135 KB
    XMLfile.doc ‏135 KB

Maybe you are looking for

  • Touch not being recognized by computer

    I went to update my touch today from my laptop and it wasnt even recognizing that it was connected to a computer. I tried my ould nano and it works just fine, My touch connects to my old desktop. Ive reinstalled itunes on here to get the newest versi

  • Unable using    hz_cust_account_site_v2pub.update_cust_site_use

    Hi All, I am facing an issue in using an API hz_cust_account_site_v2pub.update_cust_site_use. I want to update WAREHOUSE_ID from the table HZ_CUST_SITE_USES_ALL (R12), the below code is what i am using the procedure shows as completed successfully bu

  • Select statement - Where Condition not possible

    Hi, I am trying to extract data from sap standard table CFX_COL.I want extract based on a field which is of type string.I can see that in the table but when I code below I am getting error.Any other alterantives? REPORT x. TABLES:proj. DATA: it_proj

  • Insert large Image object into Database

    Hello I have a large (!MB) image which needs to be stored in a database BLOB column, and I'm running out of memory with my naive code.. Nothing other than the code is within my control. I cannot change my heap size; I have no file-system, so cannot w

  • FCP renders different part of footage

    I see that other people have had this prob in the past but I don't see any solutions... Everything plays fine in my timeline, but when timeline is rendered or exported, FCP substitutes a different part of the footage, so I end up with all these rando