header-files

How to use the tool include-what-you-use together with CMake to detect unused headers?

二次信任 提交于 2019-12-02 14:28:32
The tool include-what-you-use can be used to detect unneeded headers. I am using CMake for my C++ software project. How can I instruct CMake to run include-what-you-use automatically on the source files of my software project? CMake 3.3 introduced the new target property CXX_INCLUDE_WHAT_YOU_USE that can be set to the path of the program include-what-you-use . For instance this CMakeLists.txt cmake_minimum_required(VERSION 3.3 FATAL_ERROR) add_executable(hello main.cc) find_program(iwyu_path NAMES include-what-you-use iwyu) if(NOT iwyu_path) message(FATAL_ERROR "Could not find the program

How a standard library differ from user defined header file (.h) and its implementation file (.c) in C?

廉价感情. 提交于 2019-12-02 13:40:26
How a standard library like libc.a (static library) which is included using #include <stdio.h> in our main.c differ from user defined header file (cube.h) included in main.c with its implementation file (cube.c) in C ? I mean both are header files but one's implementation is a static library (.a) and others is source file (.c) . You would have the definition (implementation) in, say, cube.c #include "cube.h" int cube( int x ) { return x * x * x; } Then we'll put the function declaration in another file. By convention, this is done in a header file, cube.h in this case. int cube( int x ); We

Declaring a function that return a 2D array in a header file?

自作多情 提交于 2019-12-02 13:32:07
问题 I am trying to declare, within my header file, a function that returns a 2D array. How can this be accomplished, given that we already know the size of the array? Below is what I'm currently doing. class Sample { public: char[x][y] getArr(); void blah(int x, int y); private: const static int x = 8; const static int y = 2; char arr[x][y]; }; 回答1: It turns-out my original answer was totally incorrect, but I can't delete it since it's been accepted. From two separate answers below, I was able to

sharing a function between 2 .c files

…衆ロ難τιáo~ 提交于 2019-12-02 13:09:23
dir1 has dir2, file1.c and file1.h. dir2 has file2.c Now, if I want to access a function defined in file1.c in file2.c, I need to declare it in file1.h and include file1.h in file2.c -- is that a valid assumption? If no, please explain. If yes, even after doing that I am getting "undefined reference to function" error. file2.c:29: undefined reference to `function' collect2: ld returned 1 exit status * Error code 1 Compiling a c program happens in two steps basic steps: compiling and linking. Compiling turns source code into object code, and linking puts object code together, and ties all of

C++ headers redefining/declaring mixup

邮差的信 提交于 2019-12-02 11:52:38
I'm trying to abstract out a method from a simple program. This method tests the length of an array against a predeclared CAPACITY constant, and spits out an error message if conditions aren't met. However, I'm having trouble creating a header file with a .cpp file to hold the method. the header file: //arrayHelper.h #ifndef ARRAYHELPER_H #define ARRAYHELPER_H void arrayLengthCheck(int & length, const int capacity, string prompt); #endif // ARRAYHELPER_H the code file: //arrayHelper.cpp #include <iostream> #include <string> #include "arrayHelper.h" using namespace std; void arrayLengthCheck

C++ class definition split into two headers?

*爱你&永不变心* 提交于 2019-12-02 11:39:27
问题 Is it possible in C++ to split the definition of class members in two headers? What would be the appropriate way to code it? For instance: a1.h class A { public: int var; void foo1(int b); } a1.cpp #include "a1.h" void A::foo1(int b) { cout << b; } a2.h [extend] class A { public: void foo2(double c); } a2.cpp #include "a2.h" void A::foo2(double c) { cout << c; } 回答1: You can't extend a class that way, but you can use the pimpl pattern: class A { public: void foo1(int b); private: AImpl* pimpl

Defining a member function inside the class instead of outside in C++?

纵饮孤独 提交于 2019-12-02 11:39:24
问题 By writing the definition of a member function inside the class in the header file as follows, I was told that compiler treats this as an inline function - hence , anywhere it encounters this function being called in the program , it enters the complete body of the function every time instead of jumping to the one location where the function resides in memory. Is this true? class myClass { public: void Fun1() { //implemented here } } 回答1: I was told that compiler treats this as an inline

Duplicate Symbol in nested namespace

喜欢而已 提交于 2019-12-02 10:58:08
I am working on a library that I'm using in my other projects and I have the following header file: #pragma once #include <iostream> #include <map> #include "my_library/core/Structures.h" namespace My_Library { namespace NodeReaders { namespace HumanReadable { char charBuffer[256]; unsigned int uintBuffer; unsigned long long microsecondBuffer; unsigned int getNextUInt(std::istream & is) { /// Implementation } unsigned long getNextMicroseconds(std::istream & is) { /// Implementation } ... }; // namespace HumanReadable }; // namespace NodeReaders }; // namespace My_Library I've tried to include

Namespaces and includes generate link error

家住魔仙堡 提交于 2019-12-02 08:13:53
I was playing around with namespaces when I encountered a lnk2005 error. I can't figure out how to get around the error. Here's the error: 1>Source.obj : error LNK2005: "int Chart::Bars::d" (?d@Bars@Chart@@3HA) already defined in Chart.obj 1>Source.obj : error LNK2005: "class foo Chart::l" (?l@Chart@@3Vfoo@@A) already defined in Chart.obj 1>Source.obj : error LNK2005: "int Chart::t" (?t@Chart@@3HA) already defined in Chart.obj 1>C:\Users\bnm\dev\examples\play\nmspca\Debug\nmspca.exe : fatal error LNK1169: one or more multiply defined symbols found 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:00

Namespaces and includes generate link error

我的梦境 提交于 2019-12-02 07:07:54
问题 I was playing around with namespaces when I encountered a lnk2005 error. I can't figure out how to get around the error. Here's the error: 1>Source.obj : error LNK2005: "int Chart::Bars::d" (?d@Bars@Chart@@3HA) already defined in Chart.obj 1>Source.obj : error LNK2005: "class foo Chart::l" (?l@Chart@@3Vfoo@@A) already defined in Chart.obj 1>Source.obj : error LNK2005: "int Chart::t" (?t@Chart@@3HA) already defined in Chart.obj 1>C:\Users\bnm\dev\examples\play\nmspca\Debug\nmspca.exe : fatal