header-files

CPP | .h files (C++)

久未见 提交于 2019-12-05 03:21:48
I was just wondering what the difference between .cpp and .h files is? What would I use a header file (.h) for and what would I use a cpp file for? In general, and it really could be a lot less general: .h (header) files are for declarations of things that are used many times, and are #include d in other files .cpp (implementation) files are for everything else, and are almost never #include d Technically, there is no difference. C++ allows you to put your code in any file, with any format, and it should work. By convention, you put your declarations (basically, that which makes up your API)

Why could a header file be including itself?

我的未来我决定 提交于 2019-12-05 02:51:52
问题 Can anyone think of any scenario in which a header file includes itself? I saw it in one of the program and this inclusion is under conditional compilation block for which at least i could not find any true condition.But, I was thinking could there be any technical requirement for such scenario? 回答1: Yes, if you are trying to win the International Obfuscated C Code Contest. Here's a nice example (the source file is called isaak.c ): main(){} #define P define #P U ifdef #P main Si #U y #undef

Is an #include before #ifdef/#define Include-Guard okay?

ⅰ亾dé卋堺 提交于 2019-12-05 02:38:13
I always placed my #include after the #ifdef / #define Include-Guard. Now the refactor mechanism of my IDE (Qt Creator) put it before the Include-Guard e.g. #include "AnotherHeader.h" #ifndef MYHEADER_H #define MYHEADER_H Can this cause any problems or can I leave it this way? If the header in question has include guards itself, you won't run into problems. Putting it inside the include guards may still speed up compilation. Something the compiler does not see takes less time to compile, even if it does not produce any errors. Although less common, I believe this is considered to be acceptable

How to use extern cuda device variables

你离开我真会死。 提交于 2019-12-05 02:11:25
问题 I need to write the code into several .cu files. But where should I define the device variables which are use for many .cu files. An example File common.h __device__ int x; File A.cu __global__ void a() File B.cu __global__ void b() a(),b() both use x. what should I do? In C language, I should write something like extern device int x; Then I define device int x in another place. But in CUDA I can not do it. If I do, it tells me ‘..........’ previously declared here 回答1: EDIT : @talonmies was

Why /usr/include/linux/stddef.h is empty?

房东的猫 提交于 2019-12-05 01:38:01
This header file shall define NULL or size_t and other macros, but I find that /usr/include/linux/stddef.h is empty? Why? The actual location of the headers is implementation defined. What you look at is not the typical <stddef.h> included by gcc. You can find out exactly where it's located on your system with: gcc -E - <<<'#include<stddef.h>' | grep stddef.h which is equivalent to including the header <stddef.h> in an empty C file and running gcc -E on it. The headers in /usr/include/linux are used to compile the C library (usually glibc on Linux). The GNU manual says: The linux , asm and asm

Install ruby headers with rvm

心不动则不痛 提交于 2019-12-05 01:10:30
问题 Travis CI uses RVM to provide Ruby, but it doen't seem to contain the headers: $ find /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/i686-linux /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/i686-linux /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/i686-linux/zlib.so /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/i686-linux/dl.so /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/i686-linux/syck.so /home/vagrant/.rvm/rubies/ruby-1.9.3-p194/lib

Why include related header first?

萝らか妹 提交于 2019-12-04 23:44:35
On the question C/C++ include file order/best practices , the best answer recommends to include the related header first. Same for Google and Mozilla style guides. However, in both cases, I couldn't find a good reason why you would do this. Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first. This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me. But following that rationale, why would you include

Declaring variables in header files C++ [duplicate]

醉酒当歌 提交于 2019-12-04 23:29:15
This question already has an answer here: What should go into an .h file? 12 answers I'm trying to create a simple program that takes input from the user in C++ using good programming practices. It consists of Input.hpp, Input.cpp and main.cpp. I keep getting a multiple definition error even though I am using ifndef to prevent that. Input.hpp #ifndef Input_HPP #define Input_HPP #include <string> #include <vector> using namespace std; vector<string> Get_Input(); vector<string> input_array; string starting_position; int input_number; #endif Input.cpp #include <iostream> #include <cmath> #include

Do template specialisations belong into the header or source file?

落爺英雄遲暮 提交于 2019-12-04 22:33:34
During compilation I get a "multiple definition" error, which refers to a template specialisation in a header file. Do I need to put the specialisations into the source file? If it is functions you have specialized, you can either put them in the .cpp file, or make them inline in the header. Like James points out, if you don't make the functions inline, you still have to declare the specializations in the header. Otherwise the compiler doesn't know it has to look for them elsewhere. You can then put the implementations (definitions) in a .cpp file. Just like with other functions. No, you don't

What difference does it make when I include <limits> or <limits.h> in my c++ code

我的未来我决定 提交于 2019-12-04 21:52:11
Can somebody please explain this? #include <iostream> #include <limits.h> or #include <iostream> #include <limits> <limits> is a C++ Standard Library header providing similar insights to the C header <limits.h> (which is also available in C++ as <climits> ), but it is written in a way that's more useful and safe in C++ programs: say you have a template <typename Numeric> ... , and the code inside wants to know the minimum and maximum value of the Numeric type parameter that the user instantiated your template with: you can use std::numeric_limits<Numeric>::min() and ...::max() ; if you wanted