CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Nov 2006
    Posts
    318

    compiling npcap tutorial code

    I can't seem to get this code snippet to compile..

    Code:
    int main()
    {
        pcap_if_t* alldevs;
        pcap_if_t* d;
        int i = 0;
        char errbuf[PCAP_ERRBUF_SIZE];
    
        /* Retrieve the device list from the local machine */
        if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,
            NULL /* auth is not needed */,
            &alldevs, errbuf) == -1)
        {
            fprintf(stderr,
                "Error in pcap_findalldevs_ex: %s\n",
                errbuf);
            exit(1);
        }
    
        /* Print the list */
        for (d = alldevs; d != NULL; d = d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }
    
        if (i == 0)
        {
            printf("\nNo interfaces found! Make sure Npcap is installed.\n");
            return;
        }
    
        /* We don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);
     }
    I got the error: 'main': function must return a value on line 69. I added the library to the project I don't know what to do now about the error.

  2. #2
    Join Date
    Nov 2018
    Posts
    165

    Re: compiling npcap tutorial code

    > I got the error: 'main': function must return a value on line 69.

    So just add this to the end of main
    return 0;

  3. #3
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    Quote Originally Posted by salem_c View Post
    > I got the error: 'main': function must return a value on line 69.

    So just add this to the end of main
    return 0;

    That threw a lot of errors.

    Code:
    Severity	Code	Description	Project	File	Line	Suppression State	Details
    Error	LNK1120	2 unresolved externals	Project2	C:\Users\rooth\source\repos\Project2\x64\Debug\Project2.exe	1		
    Severity	Code	Description	Project	File	Line	Suppression State	Details
    Error	LNK2019	unresolved external symbol pcap_freealldevs referenced in function main	Project2	C:\Users\rooth\source\repos\Project2\Project2\Source.obj	1		
    Severity	Code	Description	Project	File	Line	Suppression State	Details
    Error	LNK2019	unresolved external symbol pcap_findalldevs_ex referenced in function main	Project2	C:\Users\rooth\source\repos\Project2\Project2\Source.obj	1		
    
    Severity	Code	Description	Project	File	Line	Suppression State	Details
    Warning	LNK4272	library machine type 'x86' conflicts with target machine type 'x64'	Project2	C:\Users\rooth\OneDrive\Documents\libpcap-dev\Lib\wpcap.lib	1
    I thought I added libpcap to the project properly, I'm at a loss.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,916

    Re: compiling npcap tutorial code

    Which compiler are you using as there is now no requirement for main() to explicitly return a value (this only applies to main - other functions must return an appropriate value)? If an explicit value return is not provided then a return value of 0 is used.

    The last error provides a clue. The machine type of the .lib must match the machine target of the project. In this case the .lib file is for x86 and your project is for x64. Try changing the target of your program to x86.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    Quote Originally Posted by 2kaud View Post
    Which compiler are you using as there is now no requirement for main() to explicitly return a value (this only applies to main - other functions must return an appropriate value)?
    Visual C++ 2022. I got this code from here. I wanna get the first to compile as I learn how to code with npcap. trying to compile this causes this error after adding libpcap to the project settings.

  6. #6
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    Quote Originally Posted by salem_c View Post
    > I got the error: 'main': function must return a value on line 69.

    So just add this to the end of main
    return 0;
    EDIT:
    Also this doesn't work, it gives the same error building. Altering this code messes it up.

  7. #7
    Join Date
    Nov 2018
    Posts
    165

    Re: compiling npcap tutorial code

    No, fixing the compiler error exposed the linker errors.

    List from the SDK
    Code:
            0  2025-01-21 10:58   Lib/
            0  2025-01-21 10:58   Lib/ARM64/
        10166  2025-01-21 10:18   Lib/ARM64/Packet.lib
        26362  2025-01-23 12:03   Lib/ARM64/wpcap.lib
        10360  2025-01-17 17:33   Lib/Packet.lib
        26940  2025-01-23 12:02   Lib/wpcap.lib
            0  2025-01-21 10:58   Lib/x64/
        10166  2025-01-21 10:18   Lib/x64/Packet.lib
        26362  2025-01-23 12:03   Lib/x64/wpcap.lib
    Point your project to x64/wpcap.lib

  8. #8
    Join Date
    Jul 2025
    Posts
    8

    Re: compiling npcap tutorial code

    Ah, I see what?s happening. That error is coming from the fact that your main() function is declared to return an int, but in some code paths, there?s no return statement at the end.

    In your snippet, you have a return; inside the if (i == 0) block. But return; alone is only valid for functions returning void. Since your main() is int main(), every possible path through the function must return an integer. That?s why the compiler complains.

    The fix is simple conceptually: make sure every exit point of main() returns an integer, usually return 0; for successful completion or another integer for errors.

    Also, minor note: exit(1); works too, but using return consistently can make the compiler happier and more portable.

    If you want, I can explain exactly where to put the return statements in your snippet so it compiles cleanly without changing the logic. Do you want me to do that?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,916

    Re: compiling npcap tutorial code

    Quote Originally Posted by natashasturrock View Post
    Ah, I see what?s happening. That error is coming from the fact that your main() function is declared to return an int, but in some code paths, there?s no return statement at the end.

    In your snippet, you have a return; inside the if (i == 0) block. But return; alone is only valid for functions returning void. Since your main() is int main(), every possible path through the function must return an integer. That?s why the compiler complains.

    The fix is simple conceptually: make sure every exit point of main() returns an integer, usually return 0; for successful completion or another integer for errors.

    Also, minor note: exit(1); works too, but using return consistently can make the compiler happier and more portable.

    If you want, I can explain exactly where to put the return statements in your snippet so it compiles cleanly without changing the logic. Do you want me to do that?
    Good catch!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    Quote Originally Posted by natashasturrock View Post
    If you want, I can explain exactly where to put the return statements in your snippet so it compiles cleanly without changing the logic. Do you want me to do that?
    Yes I would much appreciate help. These example codes are from another program I think like you explained. There's no way to really start. >_<

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,916

    Re: compiling npcap tutorial code

    Something like this:

    Code:
    int main()
    {
        pcap_if_t* alldevs;
        pcap_if_t* d;
        int i = 0;
        char errbuf[PCAP_ERRBUF_SIZE];
    
        /* Retrieve the device list from the local machine */
        if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,
            NULL /* auth is not needed */,
            &alldevs, errbuf) == -1)
        {
            fprintf(stderr,
                "Error in pcap_findalldevs_ex: %s\n",
                errbuf);
            return 1;    /* CHANGE HERE*/
        }
    
        /* Print the list */
        for (d = alldevs; d != NULL; d = d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }
    
        if (i == 0)
        {
            printf("\nNo interfaces found! Make sure Npcap is installed.\n");
            return 2;   /*CHANGE HERE*/
        }
    
        /* We don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);
    /*DON'T NEED RETURN HERE AS ASSUMES RETURN 0 AT MAIN FUNCTION END*/
     }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    Quote Originally Posted by 2kaud View Post
    Something like this:

    Code:
    int main()
    {
        pcap_if_t* alldevs;
        pcap_if_t* d;
        int i = 0;
        char errbuf[PCAP_ERRBUF_SIZE];
    
        /* Retrieve the device list from the local machine */
        if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,
            NULL /* auth is not needed */,
            &alldevs, errbuf) == -1)
        {
            fprintf(stderr,
                "Error in pcap_findalldevs_ex: %s\n",
                errbuf);
            return 1;    /* CHANGE HERE*/
        }
    
        /* Print the list */
        for (d = alldevs; d != NULL; d = d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }
    
        if (i == 0)
        {
            printf("\nNo interfaces found! Make sure Npcap is installed.\n");
            return 2;   /*CHANGE HERE*/
        }
    
        /* We don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);
    /*DON'T NEED RETURN HERE AS ASSUMES RETURN 0 AT MAIN FUNCTION END*/
     }

    I managed to get this to build. I had to add to follow the directions from here. Thanks for the help!
    Last edited by dellthinker; September 16th, 2025 at 11:23 PM.

  13. #13
    Join Date
    Jul 2025
    Posts
    8

    Re: compiling npcap tutorial code

    You are getting the error because your main function is defined to return int, but it does not return a value at the end. Just add a return 0; before teh closing brace so the compilor knows the program finished successfully.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,916

    Re: compiling npcap tutorial code

    Quote Originally Posted by natashasturrock View Post
    You are getting the error because your main function is defined to return int, but it does not return a value at the end. Just add a return 0; before teh closing brace so the compilor knows the program finished successfully.
    main() doesn't require a return at the end of the function. If there is no return at the end then return 0 is assumed. This only works for main(). The original error was that there was a return without value within the body of the function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Nov 2006
    Posts
    318

    Re: compiling npcap tutorial code

    The npcap source code will not run unless you install the executable provided on their website. saying that leads me to believe that they expect developers to use it. Digging through it there are a lot of .bat files that make up a .dll that the above source I pasted needs to execute even after successfully building.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured