Sponsored Content
Full Discussion: C header file and extern
Top Forums Programming C header file and extern Post 302851843 by jim mcnamara on Monday 9th of September 2013 11:17:18 PM
Old 09-10-2013
Here is how extern works.

You have three C files. File1 declares the variable foo (we are just doing variables, functions work the same).

Code:
int foo;

File2 and File3 both use the variable. It "lives" in File1. So it is "extern"[al] to the scope of those two C files.

To reference foo and have the linker do its magic, each of File2 and File3 has to have an

Code:
extern int foo;

Somewhere. That somewhere can be directly in the code of both files: file2 and file3. It can also be in a header file that is common to both files.

Using
Code:
#define

operations you can make all 3 files: file1 file2 and file3 see it correctly. Just using myheader.h

myheader.h:
Code:
#ifndef MAINFILE
int foo;
#else
extern int foo;
#endif

File1.c:
Code:
// prevent File1.c from "seeing" the extern version of foo by defieing MAINFILE
#define MAINFILE
#include "myheader.h"

File2.c:
File3.c:
Code:
#include "myheader.h"

This is an example, not best practice necessarily.
This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

extern for functions

Hi, Please let me know if the extern keyword is necessary for using functions which is defined and declared in another file and and used in a different file where both these files are linked together. thanks (8 Replies)
Discussion started by: naan
8 Replies

2. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

3. Programming

Extern variable.

file1.c int a1; int main() { a1 = 2; printf("\na1 = %d\n", a1); next(); printf("\na1 = %d\n", a1); next1(); printf("\na1 = %d\n", a1); } file2.c #include <stdio.h> int b1 = 0; void next(void) (1 Reply)
Discussion started by: Tanvirk
1 Replies

4. Linux

Problem mounting extern hd (fedora 9)

Hi there, I'm having a bit of a strange problem which I would appreciate some help with. The Problem: I have two external hard drives, but I'm borrowing one off my parents to copy data too (one of mine, which is identical to theirs - WD MyBook 300g - is on its way out). Fedora 9 recognizes... (3 Replies)
Discussion started by: lasthidingplace
3 Replies

5. UNIX for Dummies Questions & Answers

fetchmail and forward to an extern address

Hi, I'm searching for an solution for the following problem. I want fetch some mails via pop3 from a@a.com with fetchmail. That works perfectly. Now any incoming mail should forwarded to b@b.com via smtp obv. But I don't know how to configure that. All online tutorials describe forwarding to... (0 Replies)
Discussion started by: mcW
0 Replies

6. Programming

segmentation fault for extern

Why this is happening when both of them compiled together and run? I am getting segmentation fault SIGSEGV. File1.c: int arr; File2.c: extern int *arr; int main() { arr = 100; } (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. Shell Programming and Scripting

Comparing one file header with another file header

Hi Experts, In our project we have requirement where in we have to compare header of one file with header in the parameter file. There are 20 files which we ftp from one site. All this files have different header. We are comapring this file with our parameter file(which is having the header... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

8. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

9. Programming

Extern keyward on function in C

I saw a header (.h) file with mixture of "regular" function declarations and other extern function declarations. As I was told all function declarations are implicitly external and the extern on functions declarations is superfluous. Here my focus is on function declaration, not variable yet. int... (2 Replies)
Discussion started by: yifangt
2 Replies

10. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies
STOMP_ACK(3)								 1							      STOMP_ACK(3)

Stomp::ack - Acknowledges consumption of a message

       Object oriented style (method):

SYNOPSIS
public bool Stomp::ack (mixed $msg, [array $headers]) DESCRIPTION
Procedural style: bool stomp_ack (resource $link, mixed $msg, [array $headers]) Acknowledges consumption of a message from a subscription using client acknowledgment. PARAMETERS
o $link -Procedural style only: The stomp link identifier returned by stomp_connect(3). o $msg - The message/messageId to be acknowledged. o $headers -Associative array containing the additional headers (example: receipt). RETURN VALUES
Returns TRUE on success or FALSE on failure. NOTES
Note A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction. Tip Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached. EXAMPLES
Example #1 Object oriented style <?php $queue = '/queue/foo'; $msg = 'bar'; /* connection */ try { $stomp = new Stomp('tcp://localhost:61613'); } catch(StompException $e) { die('Connection failed: ' . $e->getMessage()); } /* send a message to the queue 'foo' */ $stomp->send($queue, $msg); /* subscribe to messages from the queue 'foo' */ $stomp->subscribe($queue); /* read a frame */ $frame = $stomp->readFrame(); if ($frame->body === $msg) { /* acknowledge that the frame was received */ $stomp->ack($frame); } /* remove the subscription */ $stomp->unsubscribe($queue); /* close connection */ unset($stomp); ?> Example #2 Procedural style <?php $queue = '/queue/foo'; $msg = 'bar'; /* connection */ $link = stomp_connect('ssl://localhost:61612'); /* check connection */ if (!$link) { die('Connection failed: ' . stomp_connect_error()); } /* begin a transaction */ stomp_begin($link, 't1'); /* send a message to the queue 'foo' */ stomp_send($link, $queue, $msg, array('transaction' => 't1')); /* commit a transaction */ stomp_commit($link, 't1'); /* subscribe to messages from the queue 'foo' */ stomp_subscribe($link, $queue); /* read a frame */ $frame = stomp_read_frame($link); if ($frame['body'] === $msg) { /* acknowledge that the frame was received */ stomp_ack($link, $frame['headers']['message-id']); } /* remove the subscription */ stomp_unsubscribe($link, $queue); /* close connection */ stomp_close($link); ?> PHP Documentation Group STOMP_ACK(3)
All times are GMT -4. The time now is 12:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy