Visual Studio referencing header and source files - c

I have a C project in visual studio that is named "Framework".
The Framework project is supposed to hold common code that should be shared among different projects.
I also have another project (Lets call it A) that should use code from the Framework.
Lets say the Framework project has a header called DAL.h and inside it a declaration of a function called OpenFile, this function is implemented in DAL.c.
I am trying to include DAL.h to my A project and use the OpenFile but I am receiving the following error: unresolved external symbol _OpenFile
I do understand that the compiler failed to find the implementation of OpenFile.
My question is what is the best practice in a case like the one described here ?
Should I "Add existing item" and add DAL.c to my A project ? (This fixes the error ..)
I did try to add the folder containg DAL.c to "Additional source folders" but that did not help.
Thanks a lot,
Michael.

For you to use the compiled code of the OpenFile function, it will need to exist somewhere.
You seem to have two options:
1) If you want the compiled code to be part of your binary, you will need to include the source file so it can be compiled as part of your project.
2) If you don't want it to be built into your project, and from the sound of it you don't as you say it's "common code that should be shared among different projects", the "Framework project" should compile into a library. In which case you will need to include just the DAL.h header file, and import the "Framework project" library into your project so that your project can link the OpenFile call to the implementation in the library.

Related

Can't include C-files in Swift-package with Xcode 11 package manager

I created a Swift 5.1 app with Xcode 11.0 which contains c-files and h-files.
A bridging header was automatically generated by Xcode.
I entered the necessary #imports.
The project compiles and runs as expected.
Now I wanted to create a Swift-package which contains the c-files and header-files, the bridging-header file and a Swift file which is a wrapper of the c-functions.
I did it in the following way.
I opened file->new->Swift Package
I filled out the name field of the package and the add to: and group: fields
Now the Package.swift appeared in the project.
I moved the Swift files, from the project folder into the Package.swift/Sources/packageName folder.
Just for test reasons I changed to the target/General window and pressed the + Button of Frameworks, Library and embedded Content.
In the appearing List of frameworks I could find the name of my package, as expected.
I canceled the window and went back to move a single c-file into the package folder.
Then I went back to the target/General window and pressed the + Button again and now surprisingly the list doesn't contain my package name
It seems to me, that including c-files into a Swift-package requires some additional steps.
I searched in the documentation, but I couldn't find a hint.
Basically you cannot at this time have mixed C and Swift sources in the same package.
You can, however, have one target that includes all of your C-files, then have a different target that includes your Swift files but depends on your C-file target.
From your question it seems like you are missing a lot of intermediate steps. If I were you I would try to get a simple example working with just the C-files and then move on to adding the Swift target that depends on that.

How to include header AND source files folder in Visual Studio

I am using Visual Studio (2017) and I need the following.
I have a folder where a code generator puts the .h and .c files obtained from a formal model. This folder is not controlled by me, e.g. I cannot write in it, but it is updated by another team member.
By using the /I compiler options (or Additional include directories in the project properties) I managed to import all the generated header files in my VS project. What I am supposed to do is to integrate this generated code into a specific platform, this means that I have to compile both the generated code and the integration code on the target platform. The problem is, the compiler is not able to resolve the generated function definitions of the generated code as it only sees the .h files. What I got is a linking error (external symbol not resolved)
To solve the problem, I added the existing .c files manually, one by one. The obvious problems that comes with this solution are
manual boring work
when new files are generated, I need to manually import the new files
Question is: is there an option that can be set in order to specify the path of the source files without passing them one by one?
note: just copying and pasting the generated code in the VS project folder is not an acceptable solution.
Thanks
If you look at https://learn.microsoft.com/en-us/cpp/ide/working-with-project-properties?view=vs-2017 then you see there is a Source Directories property that has $(VC_SourcePath) as a default but (I think) to which you can add additional paths. The documentation is unclear whether that means all source files in such a path will be included for compilation.
At the bottom of the documentation it explains how to override certain project properties by providing an external properties file. It seems you can override the targets/sources using such a file. You can generate the file using a small tool that reads the filenames in those directories and adds them to the file.
You could also analyze the .vcproj file and build a small tool that wil re-write the part with your generated source directories, reading the filenames in those directories and adding them to the section in the .vcproj file.

Using multiple main files in VS2017

I've downloaded VS2017 Community Edition and I'm working through "Programming in C" by Stephen G. Kochan.
I'd like to store all the examples in one project (possibly 1 project per chapter), however I've come across the error about multiple "main" files in one project. Before telling me I can't have multiple main's in a project, could you suggest the best way to organise these small C programs in VS2017.
I love the fact that I don't have to use a terminal compiler and would like to use VS2017 to work my way through the book.
Any suggestions?
A typical case of "assignments" in a course. In that case you can create one module (c file) for each assignment and call the current assignment from your main. Now you have all assignments in a single project, so you can easily look them up. Assignments completed you can comment-out.
I don't think you really want them all in one project. I think you want them all in one Solution, with each example in its own Project. That way you can switch between projects in the IDE and build/run/debug whichever one you are working on at the time.
Add a new project to your solution by right-clicking the solution in Solution Explorer and selecting Add->New Project from the menu.
Set the active project by right-clicking the project in Solution Explorer and choose "Set as startup project" from the menu.
This allows you to build each example on its own, or build all of them at once. Each project is self-contained and generates its own executable, and you can navigate around from one project to another inside the IDE.
You can compile and run your C files individually from command line.
This is not ideal when using an IDE.
Your other option is to use add_executable command in cmake
Adds an executable target called to be built from the source
files listed in the command invocation. The corresponds to the
logical target name and must be globally unique within a project. The
actual file name of the executable built is constructed based on
conventions of the native platform (such as .exe or just
).

Include tcl.h into C project

I need to use tcl.h in my C project (VS 2012) for registering function in SQLite. I have downloaded Tcl for windows and installed it. But my project still doesn't recognize when I write #include . What should I do to get access to this header?
Thanks in advance.
Open the project properties and do :
Right click on your project, go to properties, go to C/C++ session. On the field Additional Include Directories, add the path to the header you want, and use the header with "".
After that, you should go to the Linker session. On General, go to the Additional Library directories and add the path to the libs you need to add. Then go to Input and add the lib.
Sometimes you will need to copy a dll to the same folder of your executable. You can do that on Custom Build Step - Post Build Events.
You need to do it for each configuration and platform you want to build - one tip to make it easier is to use those variables $(Configuration) and $(Platform) when you are specifying the paths, and select on the dropdown list of the top of the properties window All configurations and All platforms, so you don't need to do it several times per project.

Forcing eclipse indexer to always use specific header file when opening declaration

I've got a very huge C project that is meant to be built on Solaris. I use a Windows PC for most of my work, ssh'ing into a solaris box when I need to make edits and build the code.
I've recently imported the project into Eclipse (Luna) on my Windows machine from GitHub for ease of traversing the code. One problem I'm having is that the indexer cannot seem to determine which header file a particular declaration is coming from... I mean it's not able to tell that an enum in alarm.c can be found in alarm.h (as alarm.h is included in alarm.c), because that same enum (ERROR, for example) can be found in a couple other header files in another part of the project. See the image for what happens when I press F3 on one of the enums with multiple declarations in the project:
Is there a way to force the indexer to always use alarm.h when it has anything to do with alarm.c source code? Can I have it ignore the rest of the project? It would really aide in getting rid of the thousands of "errors" in the project which are specifically related to "multiple declarations found" by the indexer.

Resources