• 0

[C++] extern keyword & "void" parameter?


Question

Howdy. Just some code I'm curious about... (just trying to get my head around understanding it)

extern void ThreadTest(void); // main.cc

Q1) I've done some googling about the keyword extern, but would just like some clarification! One site defines it as "an assertion to the compiler that the definition for the object or function is in another compilation unit or file". But how does the compiler know where this function is defined exactly? (I've found the ThreadTest() function in the class thread.cc, but that is not included at the top of main.cc??).

Q2) Also are there any standard/good practices or guidelines on where or when you should use the extern keyword? I'm still a little unsure why it is necessary to use it. Why not just do an "#include someclass.h" at the top and then use the functions?

Q3) Why does the ThreadTest() function declaration (above) take void as a parameter? That looks weird to me since:

  • ThreadTest() implemented in the thread.cc class does not have any parameters!
  • How do you get a void parameter?! Unless it means that there are no parameters? But then why not just leave it blank?

Thanks for the help in advance :laugh: Some funny/strange things there for me. Bear with me, only done C++ for about 6 months! :whistle:

Link to comment
https://www.neowin.net/forum/topic/291483-c-extern-keyword-void-parameter/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

1) The definition given is pretty good. You can also use extern like: extern "C" int func();

This tells the compiler/linker that the function is externally defined as a C protoype. This is used for mixing C/C++ functions/source files.

The linker generates a list of symbols from the object files (compiled source files), and is always able to find the actual declaration/implementation of the variable/function. The compiler never attempts to build a self-contained module; it just forwards external symbols onto the linker.

  • 0
  fault said:
Q2) Also are there any standard/good practices or guidelines on where or when you should use the extern keyword? I'm still a little unsure why it is necessary to use it. Why not just do an "#include someclass.h" at the top and then use the functions?

585551025[/snapback]

Your linker will generate multiple define errors when you try to write code like this...

header.h

#ifndef header_h
#define header_h

int test;

#endif

file1.cpp

#include "header.h"

void functionA()
{
    test = test;
}

file2.cpp

#include "header.h"

void functionB()
{
    test = test;
}

You solve this problem by using the extern keyword in your header file...

header.h

#ifndef code_h
#define code_h

extern int test;  // this is now a declaration, not a definition.

#endif

and define the variable in a seperate module...

header.cpp

#include "header.h"

int test;

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Hello, My thoughts on this are mixed. Microsoft has hosted malicious code in the Microsoft Update Catalog where third party device drivers are stored; I wrote about one such incident about fifteen years ago, so if there are any other old malicious drivers floating around in the catalog, this will be a good step towards preventing any infestations from reoccurring. Another thing, which surprisingly is not mentioned in Microsoft's announcement, is that this helps protect against BYOVD (Bring Your Own Vulnerable Driver) attacks, where malware either comes with or downloads an older device drivers with vulnerabilities in it that can be exploited to gain access to kernel memory. Removing all those old device drivers from the Windows Update Catalog, potentially with all sorts of undisclosed vulnerabilities in them, means an attacker can no longer leisurely count on being able to download them from Microsoft's servers--something that may go unnoticed or ignored by security analysts. This makes the adversary attack a little more noisy, since they have to either include the device driver with the rest of their initial payload or download it from a third-party site at some point prior to beginning their BYOVD attack. On the other hand, it means that people who are looking for a specific version of an older device driver for whatever legitimate reasons, like compatibility, performance or stability, may end up going to dodgy third-party sites in search of older drivers, which increases the risk of exposure to everything from nuisance advertisements and unwanted software to actual malicious code. As for me, I have keeping copies of all the device drivers, firmware updates, etc. I have downloaded over the years, some dating back to DOS and Windows 3.x era, not just for hardware I won, but popular things like unified chipset and video card drivers, just in case I ever needed it. It might seem silly to collect such a thing, but the hardware drivers, firmware updates, and documentation are just about 2 TB in size. From my perspective, it is an inexpensive form of insurance, especially given that disk space is always getting cheaper over time. Regards, Aryeh Goretsky
    • @Raze Bold it boy. (I admit, we all did it from time to time..)
    • Fan Control V227 by Razvan Serea Fan Control is a powerful and versatile portable utility that allows you to monitor, control and customize the fans of your GPU and CPU to keep your machine cool and running smoothly. Fan Control supports a wide range of devices and hardware configurations, giving you complete control over your computer's cooling system. Fan Control backend is mainly based on LibreHardwareMonitor, an open source fork of the original OpenHardwareMonitor. This means that hardware compatiblity is entirely open for anyone to contribute, and doesn't rely on a single developer who may stop caring at some point. Combined with the plugin system, Fan Control is unlocked for many generations of hardware to come. Main features Guided setup process on first launch Save, edit and load multiple profiles Change the theme and color of the application. Multiple temperature sources ( CPU, GPU, motherboard, hard drives... ) Multiple fan curve functions, including a custom graph Mix fan curves or sensor togethers (max, min, average) Low resource usage Advanced tuning with steps, start %, stop %, response time and hysteresis FanControl V227 changelog: Allow decimal with hysteresis values Radeon Pro support through ADLX Fix a bug when cancelling the graph editing dialog Update LibreHardwareMonitorLib Download: FanControl V227 | Installer ~15.0 MB (Open Source) View: Fan Control Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Lol tf you are talking about. People are PAYING by using this app exclusively, just indirectly. Do you have any idea how much facebook charges for api access to WhatsApp which has become more or less a monopoly in e-commerce.
    • My kid tried, so I took her to Microcenter and showed her the differences in price as well as storage and specs. We bought a Windows machine. She loves it, and is off to college this fall with her gaming level windows laptop at the price of a Mac that came with a 512GB SSD, and half the RAM.
  • Recent Achievements

    • Contributor
      GravityDead went up a rank
      Contributor
    • Week One Done
      BlakeBringer earned a badge
      Week One Done
    • Week One Done
      Helen Shafer earned a badge
      Week One Done
    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      660
    2. 2
      ATLien_0
      266
    3. 3
      Michael Scrip
      235
    4. 4
      Steven P.
      164
    5. 5
      +FloatingFatMan
      149
  • Tell a friend

    Love Neowin? Tell a friend!