none
Need Help With Linker

    Question

  • I tried to compile and run a C program that I made my own header and corresponding .c files in, but I got a Linker error saying that I've got an unresolved external symbol.  I've included the header and the .c file for it into my project, and I also #incuded the header in my code, but I don't know how to tell the Linker where to find the files.

    It has two custom headers; one that has #defined macros in it only, and one that has a declared that's defined in the .c file.

    Here is the code:

    /* Measuring the Pacific Sea */
    
    #include "pacific_sea.h"
    
    int main(void)
    {
    	const int pacific_sea = AREA;
    	double acres, sq_miles, sq_feet, sq_inches;
    	char ch;
    
    	printf("\nThe Pacific Sea covers an area");
    	printf(" of %d square kilometers.\n", pacific_sea);
    	sq_miles = SQ_MILES_PER_SQ_KILOMETER * pacific_sea;
    	sq_feet = SQ_FEET_PER_SQ_MILE * sq_miles;
    	sq_inches = SQ_INCHES_PER_SQ_FOOT * sq_feet;
    	acres = ACRES_PER_SQ_MILE * sq_miles;
    	printf("In other units of measure this is:\n\n");
    	printf("%22.7e acres\n", acres);
    	printf("%22.7e square miles\n", sq_miles);
    	printf("%22.7e square feet\n", sq_feet);
    	printf("%22.7e square inches\n", sq_inches);
    
    	// This is used as a workaround for when the console window closes
    	// immediately after outputting a program when run directly from the .exe file
    	printf("Please enter a character to exit: ");
    	scanf("%c", &ch);
    	return 0;
    }

    Here is the custom header with the #defines:

    #include <stdio.h>
    
    #define AREA 2337
    #define SQ_MILES_PER_SQ_KILOMETER 0.3861021585424458
    #define SQ_FEET_PER_SQ_MILE (5280 * 5280)
    #define SQ_INCHES_PER_SQ_FOOT 144
    #define ACRES_PER_SQ_MILE 640

    Here is the header with the function prototype:

    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    inline void keep_window_open();
    
    #endif

    And here is the .c file for the above header:

    #include "my_header.h"
    #include <stdio.h>
    
    // This is used as a workaround for when the console window closes
    // immediately after outputting a program when run directly from the .exe file
    inline void keep_window_open()
    {
    	printf("Please enter a character to exit\n");
    	char ch;
    	scanf("%c", &ch);
    }

    There's no option for C code here, but yeah, it's C.

    And this is the Linker error I get:
    1>------ Build started: Project: pacific_sea, Configuration: Debug Win32 ------
    1>pacific_sea.obj : error LNK2019: unresolved external symbol _keep_window_open referenced in function _main
    1>C:\Users\Osman\programming\visual studio 2015\Projects\programming_in_c\pacific_sea\Debug\pacific_sea.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



    • Edited by DragonOsman2 Thursday, October 13, 2016 12:25 AM
    Thursday, October 13, 2016 12:19 AM

Answers

All replies

  • Try removing the inline words.

    • Proposed as answer by qing__Moderator Thursday, October 13, 2016 7:03 AM
    • Unproposed as answer by qing__Moderator Friday, October 14, 2016 10:16 AM
    • Marked as answer by DragonOsman2 Wednesday, November 30, 2016 12:50 PM
    Thursday, October 13, 2016 5:28 AM
  • Hi DragonOsman,

    Would you please share me which version of VS you used? In my VS2015 Enterprise, I have created a new c++ Console Application and copy your code to the project and run it without any problems.

    Here in my operation process:

    1). Create two headers pacific_sea.h and my_header.h;

    2). Add a keep_window_open.c file to my project;

    3). Copy the main() code;

    4). Build and run the project as the following screenshot:

    I have not done anything in your code, and it could be compiled without any problems.

    And I also refer to Viorel_’s reply, removed the inline words, and the result is the same.

    And I added the keep_window_open() function to the main function, and build the project that I have encountered the same issue as you. Then I moved the inline word and it works fine.

    If I misunderstand your issue, please feel free to let me know.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, October 13, 2016 7:04 AM
    Moderator
  • I don't have this problem with C++ programs; it's only with C programs.  And in C++, the inline keyword doesn't seem to be causing problems. 

    The main problem in C++ programs is that I get the Linker errors when trying to build from the command line.  Whether it's when having everything in one file with headers or when having separate .cpp files, I still get Linker errors when trying to do it directly from the command line (I haven't tried the Visual C++ compiler yet, but that's mainly because I don't know how to invoke the Linker for that one (or even the compiler itself, for that matter), and with Clang and Clang-CL, I'm not sure about the correct flags for using the Linker). 

    I'll try building and running it as a C++ program, too, though, just in case.

    Edit: The inline keyword does seem to be the problem.  But I don't understand why.

    And I'm using Visual Studio Community 2015.  Building it as a C++ program without the inline keyword worked.  But again, I don't understand why the inline keyword is causing a problem for me when it's not causing a problem for Oscar Wu.

    But yeah, thanks for the help.


    Thursday, October 13, 2016 12:59 PM
  • Hi DragonOsman,

    >>The main problem in C++ programs is that I get the Linker errors when trying to build from the command line.

    I have encountered the same issue as you, it works fine in VS IDE. But it failed in command line with CL compiler. I found that in a .c file, the CL compiler could not compile a function form another .c file. If I put the function into the main function, and the compile result without any problems.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, October 14, 2016 10:16 AM
    Moderator