I downloaded id3lib and placed the directory in my main.cpp directory but both g++ and visual studio give file/directory not founds and "undefined" errors
Here is my main.cpp:
#include <iostream>
#include <id3lib-3.8.3/include/id3/tag.h>
int main() { std::cout << "hi"; }
g++ main.cpp gives:
main.cpp:2:46: fatal error: id3lib-3.8.3/include/id3/tag.h: No such file or
directory
#include <id3lib-3.8.3/include/id3/tag.h>
if I use "" instead of <>, i get this error:
id3lib-3.8.3/include/id3/tag.h:32:30: fatal error: id3/id3lib_frame.h: No
such file or directory
#include <id3/id3lib_frame.h>
It's not enough to put it beside your main file. As you can see in your first approach when you used #include with <> it can't find it, that's because (copied from here) :
For #include <filename> the preprocessor searches in an implementation
dependent manner, normally in search directories pre-designated by the
compiler/IDE. This method is normally used to include standard library
header files.
You didn't tell your compiler where to look for id3lib-3.8.3/include/id3/tag.h so <> will not work for you.
Then you tried "". it found id3lib-3.8.3/include/id3/tag.h but in the tag.h there is #include <id3/id3lib_frame.h>, So back to problem with first approach, right?
What you need to do is that you need to tell your compiler/IDE where to look for these files. In visual studio click right on your project file, then properties->C/C++->General->Additional Include Directories and add the include library ($(ProjectDir)id3lib-3.8.3/include/ or maybe $(SolutionDir)id3lib-3.8.3/include/) to it. Then your first approach should work fine.
I'm a pretty novice programmer, just learning a little bit of c, but I always did it on Linux with gcc and Vim but decided to try using visual studio and I'm getting LNK2005 and LNK1169 errors, I've tried looking up the errors and how to fix them and properly use PCH because I think it would be useful to learn even if my programs are too small to make use of it.
From my understanding I need to #include "stdafx.h" at the top of my source file (called 'helloworld.c') I haven't touched 'stdafx.c' from the default that came when I create the project, I created a header file called 'bitwise.h' and it has one function in it called 'int bw()' I then have 'stdafx.h' and all I added was #include "bitwise.h" In my headerbitwise.h ive tried to include #include "stdafx.h" #include "stdafx.c" #include <stdio.h> and even not including anything. all of these break my program. The only way I can get it to compile is if i comment out//bw(); then my program compiles just fine.
here are the files that I think may be the culprit:
helloworld.c
#include "stdafx.h"
int main()
{
printf("\tHello World!\n");
getchar();
bw(); //If this line is commented out everything works just Honky-Dory
getchar();
return 0;
}
bitwise.h
#include "stdafx.h" //I've tried lots of diffrent lines here, nothing works
int bw()
{
int a = 1;
int x;
for (x = 0; x < 7; x++)
{
printf("\nNumber is Shifted By %i Bits: %i", x, a << x);
}
getchar();
return 0;
}
stdafx.c
// stdafx.cpp : source file that includes just the standard includes
// $safeprojectname$.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include "bitwise.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
This is not much about PCH. You have mixed up header (.h) and implementation (.c) files. What you need to do is split implementation and declaration. You should do following:
rename your bitwise.h to bitwise.c since this is your implementation file, not a header!
create a new file bitwise.h and put only declaration there, it should look like this:
#pragma once
int bw();
Your project should be able to compile after that.
Please also note PCH file should contain includes that are infrequently changed, which is probably not your case since you are including also bitwise.h. You might want to remove this include from stdafx.h and include it into your helloworld.c.
And just a side note, during learning C do not think of including .c file via #include! If it fixes some of your compilation errors, your project design is probably very wrong.
Nitpick: You should not need to #include stdafx.h in bitwise.h, though it should still have a #pragma once.
Your code for bw() should still be in a separate bitwise.c file, not in the header. I think you may be confusing precompiled headers with function inlining? Right now, your code for bw is being compiled into what is supposed to be the dummy stdafx object, and again in the main object, and causing a conflict when linking.
Also, did you remember to mark your stdafx.h as the precompiled header (/Yu), and the stdafx.cpp as... whatever /Yc is supposed to mean? Make sure both options are set for all project configurations for both files in Properties -> C/C++ -> Precompiled Headers.
I just moved all my header files to be in the include directory of my project, rather than having both .cpp and .h in the src directory. I'm using YouCompleteMe in vim and now when I open vim, it tells me that my header files cannot be found on the side to alert me of compiler errors. Specifically, it gives me errors on the #include "my_header.h" of my_header.h file not found and errors on each method saying use of undeclared identifier. However, the code compiles just fine. And the auto-completion works fine. It seems that clang cannot find where my header files are located now since I moved them ../include. If I change my #include line to be #include "../include/my_header", all of the errors to away. But it seems better to only have #include "my_header.h" in all of my .cpp files or #include <my_header.h>.
Can anyone help me solve this issue?
I don't really understand the why behind this, but I think I've figured out the what.
My brother is working on a homework project and they're using Visual Studio. He was having issues getting his project to compile. The project in question consisted of:
main.cpp
classes.h
class_functions.cpp
general_functions.cpp
They had the following import structure:
main.cpp
#include "classes.h"
#include "class_functions.cpp"
#include "general_functions.cpp"
class_functions.cpp
#include "classes.h"
general_functions.cpp
#include "classes.h"
He kept getting link errors when trying to compile, even though g++ would compile it just fine. So I tried via Visual Studio command prompt and cl. This compiled just fine, so I tried msbuild which (obviously?) failed. I went into the vxproj file, because hey, what is it telling cl that I'm not?
In the proj file it was including classes.h, general_functions.cpp and class_functions.cpp, as well as main.cpp. When I removed general and class functions, msbuild ran just fine. Put either of them back and boom. classes.h contains
#ifndef myclasses
#define myclasses
/* Code here */
#endif
And the other .cpp files contain similar directives.
I finally figured out how to eliminate the problem - eliminate
#include "general_functions.cpp"
#include "class_functions.cpp"
from the code base, and now Visual Studio compiles this just fine.
The question is why? What is the justification or possible benefits from doing this?
the difference between .cpp and .h files is that the .h files are usually included with the #include directive while .cpp files are included in the linking process.
The #include instruction will insert a copy of the included file in the code you are compiling, so by including the .cpp files you will compile a single source file that contains all your .cpp files. It's very bad practice but still could work.
But if you are then adding the .cpp files also in the linking step then you are adding those files a second time creating probably duplicate definitions of your functions, e.g. linker error. I think that's probably the main error (without the linker error codes I'm just guessing).
I have recently become aware that I have no idea, genericly speaking, how a c/c++ compiler works. I will admit this initialy came from trying to understand header guards but came to the realization that I am lacking in how compiling works.
Take Visual C++ for instance; Theres the "Header Files" folder, the "Resources Files" folder, and "Source Files" folder. Is there any significance to the separation of these folders and what you put in them? To me, they are all source files. Take the code snippets:
Snippet 1
//a1.h
int r=4;
and
//a1.cpp
int b //<--semicolon left out on purpose
and
//main.cpp
#include <iostream>
#include "a1.h"
void main()
{
cout << r;
}
The compiler errors out saying "a1.cpp(3) : fatal error C1004: unexpected end-of-file found" where I would expect it wouldn't because the a1.cpp file is not #included where the main method exists where in the next code snippet
Snippet 2
//a1.h
int r=4 //<--semicolon left out on purpose
and
//a1.cpp
int b = 4;
and
//main.cpp
#include <iostream>
void main()
{
cout << b;
}
Errors out because "main.cpp(6) : error C2065: 'b' : undeclared identifier". If you include the a1.cpp like so
Snippet 3
//a1.h
int r=4 //<--semicolon left out on purpose
and
//a1.cpp
int b = 4;
and
//main.cpp
#include <iostream>
#include "a1.cpp"
void main()
{
cout << b;
}
the compiler complains "a1.obj : error LNK2005: "int b" (?b##3HA) already defined in main.obj". Both snippets 2 and 3 ignore the fact that int r = 4 does not have a semicolon missing as I suspect that it has something to do with its a xxxx.h file. If I remove the a1.cpp file from the project on snippet 1, then it compiles fine. Clearly what I have expected is not what I am getting. Theres plenty of books and tutorials on how to code in cpp, but not much in the way cpp handles files and source code in the complition process. What on earth is going on here?
Your questions aren't really about the compiler, but about how your IDE is handling the entire build system. The build systems for most C/C++ projects compile each .c or .cpp file separately, and then link the resulting object files together into a final executable. In your case, your IDE is compiling any file you have in the project with a filename extension of .cpp and then linking the resulting objects. The behaviour you're seeing can be explained as follows:
a1.cpp is missing a ;, so when the IDE tries to compile that file, you get the error about 'unexpected end of file'.
b isn't declared anywhere in the main.cpp compilation unit, so you get an error about an undefined identifier.
b exists in both the main.cpp and a1.cpp compilation units (obviously in a1.cpp, and via your #include for main.cpp). Your IDE compiles both of those files - now a1.o and main.o each contain an object called b. When linking, you get a duplicate symbol error.
The important point to take away here, which explains all of the behaviour you see, is that your IDE compiles every .cpp file - not just main.cpp and the files it includes - and then links the resulting objects.
I recommend setting up a command-line test project with a makefile you create yourself - that will teach you all about the inner workings of build systems, and you can then apply that knowledge to the inner workings of your IDE.
header files are not compiled
an #include directive literally pastes the contents of the includable file instead of the #include line
All source files (redargless of main) are compiled into .o or .obj files.
All obj files are linked together along with external .lib files if there are any
You get an executable.
Regarding point 2:
example
//a.h
int
//b.h
x =
//c.h
5
//main.cpp
#include <iostream>
int main()
{
#include "a.h"
#include "b.h"
#include "c.h"
;
std::cout << x << std::endl; //prints 5 :)
}
This isn't a full answer, but hth, my2c, etc :)
Since it seems that there are two ways of understanding your question, I will answer to the understanding C++ compilation part.
I suggest that you start by reading the "compiler" definition in Wikipedia. After that, try Google search for compiler tutorials to build up your comprenhension about compilers. More specific to C++, you can read about #include and preprocessor directives (try Google search for those terms.)
If you still want to understand compilers further, I suggest a compiler book. You'll find a good list of books on StackOverflow.
The #include statement inserts that file into the file making the #include. Your snippet 3 main.cpp thus becomes the following before compilation.
// main.cpp
// All sorts of stuff from iostream
//a1.cpp
int b = 4;
void main()
{
cout << b;
}
The reason you are getting a linker error is that you are defining b twice. It is defined in a.cpp and in main.cpp.
You may wish to read about declaring and defining.
You tell the build system what files to compile. In the case of Visual C++, it will automatically compile any file named "*.cpp" that you add to the project. Though you can go into the project settings and tell it not to.
It will not compile files named *.h (though it can if you explicity tell it to.
The #include directive, is something the compiler processes before it does any compilation (it's called the pre-processor). It basically takes the file that it is pointing it to and sticks it in the source file being compiled at the point the #include directive appears in the file. The compiler then compiles that whole thing as one complete unit.
So, in your example cases:
Snippet 1
Bote a1.cpp and main.cpp are compiled seperately by the build system. SO, when it encounters the error om a1.cpp, it reports it.
Snippet 2
Note that it compiles these files seperately, with no knowledge of each other, so when your reference b in main.cpp, it does not know that b is defined in a1.cpp.
Snippet 3
Now you've included a1.cpp in main.cpp, so it compiles main.cpp, sees a definition for b and says, OK I have a b at global scope. Then it compiles a1.cpp, and says OK, I have a b at global scope.
Now the linker steps in and =tries to put a1 and main together, it;'s now telling you, hey I have 2 b's ate global scope. No good.
The compiler picks up source files from where you tell it to. In the case of Visual C++ there's an IDE telling the compiler what to do, and the different folders are there because that's how the IDE organises the files.
Also, the error in snippet 2 is from the linker, not from the compiler.
The compiler has compiled main.cpp and a1.cpp into object files main.obj and a1.obj and then the linker is trying to make an executable combining these object files, but the variable b is in both a1.obj (directly) and main.obj (via the include of a1.cpp), so you get the "already defined" error.
The problems you see in case 1 and 3 are VS specific. VS apparently tries to compile both main.cpp and a1.cpp.
Case 1: As VS tries to compile a1.cpp, which has an syntax error (the missing semicolon), the compilation fails.
Case 2: You have not declared the variable b in your main.cpp or in any included files. Thus the compilation fails.
Case 3: This is a linker error. Due to the include, int b has been declared in main.cpp as well as in a1.cpp. Since none of them is either static or extern, two global variables with the same identifier have been declared in the same scope. This is no allowed.