Trying to understand simple task of adding files to VS projects - c

As I said before, I'm still learning. Per the comments in this previous post, I'm trying to add a file into an existing project, rather than including it. The source code is listed in the previous post. I used these instructions to add the file to the project. I renamed the include file from FilMst5.c to FilMst5.cpp to avoid the following error:
C1853 'x64\Debug\CrtFil5.pch' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa)
I also added #include "stdafx.h" to prevent another compile error message. When I tried to compile the project files, I got 2 messages 'FilMstFilPtr': undeclared identifier and another message 'Opn': identifier not found. So I know I'm doing something wrong, but I don't really understand how it's supposed to work in order to fix it. Can someone tell me the correct way to do this?
The C code previously compiled fine even though it has a .cpp extension, the way Visual Studio does things.

I apologize for anyone wasting time on this question. I just needed the necessary declarations in a header file, and the linker took care of the rest of it.

Related

Can i have multiple C source files in the same project (Visual Studio 19)?

Beginner here. I code in C atm. So Ive just started using VS 2019. A few troubles along the way but I got Build error along with LNK 2005 and LNK 1169. Turns out I cant have multiple files in the same project. Especially if Im using same header in the files. In this case it was stdio.h and math.h. I recreated this multiple times. The code would compile without a hitch only if one file was present in the project. Or atleast just one file with that particular header. Which pretty much prevents me from having multiple C source files under the same project. So is there any way to have multiple files in the same project without these errors.
A little annoying that I can't have multiple files with same header which is impossible cause obviously, I gotta use stdio.h everywhere. enter image description here
Essentially a Solution is a container used to contain multiple Projects, you can choose to compile either the whole Solution (so all the projects in the solution), or a single Project in said Solution.
Each Project can have only 1 main() because otherwise your OS won't know where to start executing, of course you can have multiple source files in the same project otherwise for even smaller projects you'd have a single file with a huge number of lines.
This structure is quite common in many IDEs (maybe with different names than Project and Solution, this is just a nomenclature that VS uses), it isn't really bound to Windows or Linux in particular.
Try on Linux to have multiple source files with multiple main() and try to compile then together with gcc, you'll see that you get a very similar error.
You also can't have the same function definition in multiple files otherwise you get the LNK 2005 linker error that you've seen. This is because when the Linker is linking your files if it finds two identical definitions it doesn't know which one to take and goes crazy.
But then how do you include the same header files in different sources?
Well that's why .h files have include guards, they ensure that the contents of an .h files are included by the linker only once in each Compilation.
Usually in the header file there is one of two possible ways to include these guards, there are very small differences between these two methods but they don't really apply if you're not doing some very specific stuff:
#pragma once
// Content of the header
or
#ifndef GUARD_H
#define GUARD_H
// Content of header
#endif
Where GUARD_H should be different for every .h file, usually it's a combination of your Project and header file names just to make sure it's different to other guards that could exist in other headers.
That's also why you can't (and shouldn't) include Source files in your code, if you include a source file then you are at a high risk of including the same definition multiple times since they don't use include guards.
The C file gets stored normally in any folder you want and gets compiled with a gcc and ./a.out in the terminal
That's essentially what VS does, just instead of you choosing which files to add in the compilation VS takes automatically all the files in the same Solution or Project (depending which one you decide to build).
For convenience, if you are just starting to learn, create a new solution for each Project (so for every main()) that you want.
Is this better or worse than using a simple text editor? Well, after the first 10-15 minutes of learning you'll see that it's much faster and useful to use an actual IDE as it provides many features that will make you write code faster and with less errors.
This is a really quick explanation of how things work in general. If you can, try to find someone IRL that can explain these things to you. It's really hard to explain it on the internet but it will take less than 10 minutes being together in front of the same computer.

Where can I find missing igraph header files?

i have compiled and installed igraph according to the documentation and no errors occurred, additionally i can use and compile the library just fine (I use CLION). No the code completion does not work for some of the functions, they seem to be quite basic functions. So I checked if my IDE know the header files installed, located in /usr/local/include/igraph (i use CentOS). It knows them, so i used grep to search through them to find e.g. igraph_vector_init, and it is nowhere to be found! No of course I would like the code completion to work and I am at a loss since the appropriate header files do not seem to contain the declarations of often-used functions! Is this a problem with my build? I also checked the online repository, without avail. Additionally I checked that the examples (which use the functions) and the documentation match the version, they do. What could I possibly miss?

Linking Code::Blocks custom libraries error (cannot find)

I'm struggling with the Code::Blocks IDE when programming in C.
I use one custom library, named "main.h".
Yesterday, when I begun working on the library, the program compiled without errors. I even put some functions written on the library on my source file and it built and ran fine.
Today, when trying to recompile the whole project after some modifications, appeared the "undefined reference" error.
I searched it over on this site, I tried to follow the steps another user proposed but now, instead of the "undefined reference" error, appears the "cannot find -l[filename]".
I tried to collect information about this error on this site as well but, I understand, it's a OS problem and, apparently, never happened to other Code::Blocks users.
I use Windows 7 64 bit, in case this is the problem.
Can you please help me?
Try to put fullpath to library in build options of your project and be sure to put it in the right build scheme(Debug/Release) or both to be sure.
However it is quite strange name for library - "main.h". Is it "main.h.dll" or what? Maybe it is header file? If it is so and you put implementation of you functions in .h file and not in .c, some errors could occur.

Eclipse CDT can resolve macros and functions, but not structures or enums (Code browsing only)

I'm working on a large C project (multiple makefiles, modified third party code, etc.) and I would like to use Eclipse as a graphical front-end for GDB and for browsing/editing the code.
Right now I have a problem where Eclipse can resolve the macros and functions in the code, but not data structures or enums. Please note I am only concerned with browsing/editing the code. I will compile the project outside of Eclipse. I should also mention that Eclipse has no trouble resolving anything from a standard C library. I see that is a common question, but that is not a problem here.
So far I've done the following:
The entire code base is loaded into Eclipse.
I can launch the final application from Eclipse and interact with it in the Console.
The Indexer is enabled and it's set to index source files not included in the build, unused headers, and to not skip anything. I've run the Indexer a few times. I've also tried closing/reopening the project and restarting Eclipse.
I added the directory containing the header files for the project to Properties->Paths and Symbols->Includes. I see this directory under "Includes" in the Project Explorer".
If I'm browsing a source file, I can right click and select "Open Declaration" to see macro definitions and function definitions, even if those definitions are in other files. This is not working for data structures or enums. Eclipse reports Type "StructureX" could not be resolved?
What do I need to do so Eclipse can resolve data structures?
Update
I just looked at the parser.log file. I see "Attempt to use symbol failed:" for the structures and enum members Eclipse can't resolve, but I don't see the reason why.
I found the answer, oddly enough, because the default color scheme was bothering me. When I switched to a more soothing black background, I noticed that Eclipse thought all of the #ifdef DEFINE_NAME_X macros were undefined.
The enums and macros that the Indexer was not able to find were inside these statements.
Once I opened Properties->C/C++ General->Paths and Symbols->Symbols and defined a few key macros, the indexer was able to find the missing symbols.

unknown type name ‘gpgme_decrypt_result_t’ when trying to compile balsa

I've been trying to compile balsa the email reader on ubuntu (i'm new to the whole process of compiling an open source project directly from sources.
so i did the whole configure and make but then I ran into this problem:
error: unknown type name 'gpgme_decrypt_result_t
after doing a little research on the error code.. it turns out that it belongs to gpgme a scary looking encryption library..
what do I do in this case? i hope i'm not expected to go in the code and debug stuff am i?
update:
this question is related to this other question here on the ubuntu forum
Unfortunately, you may be required to delve into the code to figure out why something doesn't build!
In this case however the answer is probably quite simple. The main reasons I would expect a symbol from an external library to not have been defined are:
The library header files do not exist on your system
You have the wrong version of the library header files on your system
The headers exist, but the compiler is not looking in the correct place
Clearly the solutions are similar: you will need to install the correct version of gpgme's header files for this software in the right place. Note that having the library itself installed is not the same as having the headers. In Ubuntu this is evident due to the presence of -dev packages which are the package's header files, and if you look through the repose you will find libgpgme11-dev. I suggest you start by installing this, which will eliminate point 1 above. If it still doesn't work, you'll need to start investigating the other two possibilities.

Resources