Hi people:

Here is a very basic question, but it has been bugging the living hell out of me.


I am using Visual Studio 2005 and progamming in C. The whole program's skeleton was given to me, and NOT to be altered. There are several header files in the "header" folder and several source files in the "source" folder. I was supposed to create another source file called "stringQueue.c" and implement all the functions whose signatures declared in the "stringQueue.h" (already given).

So I created this "new" source file and defined those functions, and lastly I put #include "stringQueue.h" on the top of the stringQueue.c file. But when I tried to complie the program, (main() is in another source file), it gave me more errors and warnings than I could handle.
My solution was to cut all the functions in the stringQueue.c file and pasted them into the bottom of the source file where the main() was, and it went without any problems.
I really do not know how to use this #include or how to organize large program into smaller source files. Can any one you help?

Many thanks

Dani AI

Generated

A short expert follow-up to the thread (noting that later reported the immediate problem was fixed and asked for details).

Common root causes when splitting a C program into multiple files

  • The new source file is not part of the project/build, so its object never gets linked.
  • Function bodies accidentally live in headers (every including translation unit gets a duplicate definition).
  • Missing include guards or wrong include paths cause the wrong file to be compiled or multiple inclusions.
  • Global variables with conflicting linkage (missing extern or wrongly declared static).

A practical checklist to diagnose and fix

  • Confirm the .c file is actually added to the Visual Studio project (Solution Explorer -> add the file). Also verify the header is found by the compiler (Project Properties -> C/C++ -> Additional Include Directories). See Microsoft docs: .
  • Keep only declarations (prototypes, typedefs, externs, inline macros) in headers. Put exactly one implementation (function bodies) in one .c file.
  • Protect headers with include guards to avoid multiple-definition surprises. See Include guard.
  • If the build shows unresolved external symbol/linker errors, that means the compiler saw a declaration but the linker did not find a compiled definition; check that the .c file is compiled into the final link (see LNK2019 and similar errors explanation: LNK2019 unresolved external symbol).

Minimal header/implementation pattern

#ifndef MYAPI_H
#define MYAPI_H

/* public API declarations */
void foo(int);

#endif
#include "myapi.h"

void foo(int x) { /* single definition here */ }

Notes: make helpers static in a .c file to keep them local, and avoid defining objects in headers (use extern in headers and define once in a .c file). These checks explain the common situation described in the thread and should prevent the same build surprises.

Hi you guys, the problem has been solved. I guess I need more time on programming and learning.
Thanks:mrgreen:

could you tell me?
lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.