Visual Studio .NET
(1)
Linux
(1)
Windows
(1)
Unix
(1)
Makefiles
(1)
Linkers
(1)
Guards
(1)
Cpp
(1)

Porting code from Linux to Windows

Asked By Rayne
20-Nov-09 08:00 AM
Hi all,

I am using Visual Studio .NET 2003, and I am trying to port code I have
written and compiled/run successfully in Linux GCC to Windows.

I am a newbie when using VS. I have created a new project, and added all
the .c and .h files I have into the project by Project -> Add Existing
Items, then chose all the .c and .h files.

I am not familiar with how exactly compilers and linkers etc work, but
is there a difference between how VS and gcc compile/link #include
files? My habit of programming in Linux has been to have one main.c
file, and #include all other .h or .c files that I need. Then I would
only compile the main.c file. But in VS, it seems as if the #include
files are not "seen" by the program, because I am getting errors that
tell me certain structures or variables were not declared, even though
they are in my user-defined header files.

I am also getting errors like DIR is an undeclared identifier. I have
included , so why cannot it recognize DIR?

Thank you.

Regards,
Rayne

Fundamentally no - not to my knowledge anyway.

David Lowndes replied to Rayne
20-Nov-09 05:59 AM
Fundamentally no - not to my knowledge anyway.


While you can do that in either system, I would not recommend it.

You normally only #include .h files. Each .c/.cpp source file is
compiled separately, then each object file is linked together.

In visual studio, if you add all your .c & .h files to the project it
should automatically compile each source file and then link them.
Since you have adopted a practice of having everything see everything
else that would probably explain why your VS build is getting errors.
You really ought to adopt the more modular approach, but to do so
you will have to ensure that the correct headers are included from the
correct source files.


No idea, you would  have to provide some more context to the question.

Dave

Rayne wrote:Rayne:I am guessing that the reason you adopted this practice of

David Wilkinson replied to Rayne
21-Nov-09 07:11 AM
Rayne:

I am guessing that the reason you adopted this practice of #including all the
other .c files in a main.c file is that you never learned to use makefiles in
the Unix/Linux environment. While your method will work for small projects, for
large projects it becomes extremely inefficient because your entire source base
must be recompiled each time you make any change.

The way Visual Studio works is essentially by automatically creating a makefile
for you. Get rid of the #include's of the .c files and add all the files to the
project. As David Lowndes says, you will have to make sure that each .c file
contains #include's of the correct headers.

If you want to keep using the same files in your non-makefile Linux project then:

1. Make sure that all you header files have #include guards to prevent multiple
inclusion in the same translation unit.

2. Include your .c files in main.c like this

//....

--
David Wilkinson
Visual C++ MVP
Post Question To EggHeadCafe