0

This post can be closed, since someone gave an answer to my problem in the comments below. The linked solution however has nothing to do with the problem and should be removed.


I am trying to build a project containing two .c files and a header file in the Virtual-C integrated IDE and compiler.

No matter what I try, where I save the files or what I change, I always get the following error message:

linker: can't find the symbol _testReturn as requested in _main

My build options are shown below, which seem to indicate that Virtual-C is linking test.c and hence the definition of testReturn should be visible to the linker. What am I missing?

Build Options

test.h:

#ifndef TEST_H
#define TEST_H

int testReturn(int a);

#endif

test.c:

#include <test.h>

int testReturn(int a) {
     return a;
}

main.c:

#include <stdio.h>
#include <test.h>

int main(void) {
     int a = testReturn(2);
     printf("%d", a);
     return 0;
}
Alexander
  • 35
  • 7
  • Please do not post an exact duplicate of your [closed question](https://stackoverflow.com/q/71639664/11082165). If you think your question was marked as a duplicate incorrectly and should be re-opened, you can follow the steps outlined the FAQ ["This question already has answers here" - but it does not. What can I do when I think my question's not a duplicate?](https://meta.stackoverflow.com/q/252252/11082165). As a side note, your question was closed by the members of the community, not by an elected moderator. – Brian61354270 Mar 27 '22 at 21:17
  • @Brian It is not an exact duplicate. I specified a few things and changed an error in the code. – Alexander Mar 27 '22 at 21:19
  • @Brian It also told me on the closed post: "Your post has been associated with a similar question. If this question doesn’t resolve your question, ask a new one.", so that is what I did. – Alexander Mar 27 '22 at 21:28
  • What compiler are you using? How are you compiling? – Irelia Mar 27 '22 at 21:28
  • @Yuumi I have no idea. I am fairly new to coding and am simply using the Virtual-C IDE to run my code. – Alexander Mar 27 '22 at 21:33
  • @Alexander I understand your frustration. But it's important to note that asking multiple poorly received questions can lead to an [automated question ban](https://meta.stackoverflow.com/q/255583/11082165), which can _only_ be lifted by improving your previous questions. To avoid that, you'll want to make every effort to salvage your original question using the feedback you received from the community rather than posting the same problem as a new question. All the changes you made here would be better made as an [edit](https://stackoverflow.com/posts/71639664/edit) to your original post. – Brian61354270 Mar 27 '22 at 21:40
  • 1
    You don't show how you are compiling and linking your multi-file program. You must list both `main.o` and `test.o` on the linking command line, and you are apparently not listing `test.o` (because if you were, the function would not show as undefined). Headers are the glue that help keep a program coherent, but they don't affect how the program is linked and you must link it correctly. In particular, including a header `test.h` does not ensure that `test.o` is linked with the program. – Jonathan Leffler Mar 27 '22 at 21:41
  • `#include ` should be `#include "test.h"` -- See [here](https://replit.com/@robertwharvey/PurplePhonyBraces#main.c) – Robert Harvey Mar 27 '22 at 21:47
  • 1
    There are probably other, older, more verbose questions and answers that cover this situation, but the chosen duplicate is one possibility. – Jonathan Leffler Mar 27 '22 at 21:48
  • @JonathanLeffler I am simply running the code in my IDE, where it tells me in the build options: ✓ auto (to compile and link the file in the active editor window.) Select files to compile and link: ✓ Include [test.c] Options [ ]. Is there something I have to add there? There is also the option 'use additional option', with 'Build specific compiler settings'. – Alexander Mar 27 '22 at 21:51
  • @RobertHarvey Thanks for your answer! That is one of the things i tried before. It still gives me the same error message. – Alexander Mar 27 '22 at 21:56
  • You need to ask about how to configure your IDE to build and link multiple object files into a single project. I don't use an IDE — I've not yet found one that doesn't drive me bonkers, and I've tried quite a few — so I can't help you with the details. But unless the command line that's executed to create the program list both `main.o` and `test.o` (or perhaps `main.c` and `test.c`, or some mix'n'match hybrid), you will get undefined reference or missing symbol problems. – Jonathan Leffler Mar 27 '22 at 22:03
  • @Alexander It might help if you added some screenshots of your project layout and build settings. The bundled IDE and compiler you're using is frankly pretty esoteric (it doesn't appear to even have a tag on SO), so it's unlikely that anyone here has direct experience with it. We can only extrapolate from experience with typical compilers – Brian61354270 Mar 27 '22 at 22:04
  • @JonathanLeffler Thank you for the answer. I now at least understand that the problem should be with my IDE and not my code and am aware that it should work if I was to try compiling it with gcc. But I would still like to fix the issue with the Virtual-C IDE since that is what I'm comfortable with using. I don't understand how to go about that and now my question is closed again. :D – Alexander Mar 27 '22 at 22:06
  • @Brian I was not aware that you could post pictures and I'm frankly not able to keep up with all the comments as fast as you all. But I will try to post a screenshot in a bit. Thanks for the patience and the help! – Alexander Mar 27 '22 at 22:09
  • @JonathanLeffler As far as I can tell from the IDE's manual, there are no visible compiler/linker commands, so that line of debugging will lead to a dead end. Their IDE seems to ship its own integrated, home-brewed compiler, which also comes with the notice _"You should not use these tools for professional software development."_ – Brian61354270 Mar 27 '22 at 22:12
  • 1
    Maybe you need to upgrade your IDE to another. Professional software development involves using multiple source files in a given program. – Jonathan Leffler Mar 27 '22 at 22:13
  • @Brian Hm, that doesn't seem very promising. But I am not using this IDE for professional software developement, only for learning the basics of C. I also added a picture of the build options. Do you think it might work if i add something there? Thanks for all the help so far! – Alexander Mar 27 '22 at 22:29
  • A really ugly hack — not recommended — adds `#include "test.c"` at the bottom of `main.c`. That directly includes the source for `test.c` in `main.c`. It will work with this simple example, but is not guaranteed to work (e.g. because both files may have static functions with the same name, for instance, which are not visible to each other when the files are compiled separately, but which conflict with each other when they are compiled as one translation unit). But multi-file programs are fundamental to C programming, so the IDE is of (very) limited use if it can only handle one file programs. – Jonathan Leffler Mar 27 '22 at 22:35
  • 3
    @Alexander In the build options screenshot, it looks like you still have the "auto" option enabled. You'll need to turn that off to enable the build options below. From the manual: "In case you want to modularize a project into multiple C files, you need to enable the build options: Select the menu Build -> Build Options and disable the auto checkbox." – Brian61354270 Mar 27 '22 at 23:15
  • 2
    I'm voting to re-open this question since it's now apparent that it's a specific and answerable problem about how to configure the build options in Virtual-C, not a generic problem about debugging linker issues. – Brian61354270 Mar 27 '22 at 23:27

0 Answers0