Author Topic: Adding existing file to project  (Read 12100 times)

Offline Bravo685

  • Single posting newcomer
  • *
  • Posts: 2
Adding existing file to project
« on: January 22, 2013, 05:16:10 am »
Hi everyone,
I'm in need of some help on adding an existing file to an open existing project.

I want to add a header file and I have tried adding it via 'Project' - 'Add Files'. I have also tried right clicking on the project itself on the Management View on the left using 'Add Files'.
I have tried right clicking on the file in the Management View and changing the files properties. The settings I changed were 'Compile file' and 'Link file' - they are both checked for Debug and Release.
I have used 'File' - 'New' - 'File' and selecting the c/c++header - then choose my header file.

I have tried this as well,
#include <stdafx.h>
#include "stdafx.h"

So far all the options mentioned above still return the following build error: main.cpp|3|fatal error: stdafx.h: No such file or directory|

I'm sure I'm doing something wrong, but I feel that I have tried to solve the problem the best that I can. So I ask for you help. Can you tell me the correct method of adding an existing file to an open project.


Thanks everyone!

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: Adding existing file to project
« Reply #1 on: January 22, 2013, 08:00:41 am »
The "Project - Add Files" way is the correct one but you need to integrate the file in your project.

(1) If it is a source file or "your" file specific for the project then copy/move the file on the place where it should be in your project. Then do "Project - Add Files".
(3) If it is a header then update "Compiler Options - Search Paths" so that the compiler will find your header.
(4) If it is a library then update "Linker Options - Additional Libraries" so that the library will be linked by the linker.

In general, try to avoid "distributed" projects with files scattered on the disk. You get problems with archiving such project at least. Copy/move to your project directory structure then add. Headers and libraries are often in "standard places". Let them be where they are and update Search Paths and Linker Options. Also, foreign headers and libraries aren't, in fact, a part of your project so that do not "Project - Add Files" them to the project. The compiler and the linker will find them if they are instructed to search for them.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Adding existing file to project
« Reply #2 on: January 22, 2013, 08:02:13 am »
So far all the options mentioned above still return the following build error: main.cpp|3|fatal error: stdafx.h: No such file or directory|
The compiler tells you it cannot find the header file. If you look at the full build log you'll see in what folder the compiler looks for this file. If its in none of these, add an include statement accordingly. If you don't know how to setup the include folder in C::B, there is a nice section in the C::B manual, including images.

stdafx.h belongs to the MFC classes, so you need an MFC capable compiler (MSVC) plus the Windows/MFC SDK or simply replace the code that is MFC dependent.

However, this forum is not for discussing such topics as it is not a C::B issue. Please post such questions either in a C/C++ forum or in a MSVC / MFC forum.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Bravo685

  • Single posting newcomer
  • *
  • Posts: 2
Re: Adding existing file to project
« Reply #3 on: January 22, 2013, 03:43:43 pm »
Thanks everyone,

I was assuming the 'Add files' option would add the path since the file was shown in the Management view under my project. This seems confusing to me. If adding files to a project doesn't make it available to the compiler and it doesn't copy the file to the project folder. Does add files then only make the file available for editing?

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: Adding existing file to project
« Reply #4 on: January 22, 2013, 06:39:40 pm »
In fact, only source files which are part of your project need to be "project files". These files get compiled if you command "compile". Other files may or may not be "project files". These files can be only read (headers), they can be used by the linker (libraries) or they can be loaded at runtime: the current directory of the app running from Code Blocks is the project folder even if the app itself is elsewhere.

Other files are added to the project differently from "Add Files". Either add a search path for the compiler or add a library to the Linker library list. Even if you add these files to the project, you need to update the search path or the library list.