Like Tree10Likes

function calling between sources

This is a discussion on function calling between sources within the C++ Programming forums, part of the General Programming Boards category; so nothing?...

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    so nothing?

  2. #17
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    When it comes to this board I hardly get an answer I understand, but a little while ago I learned to google the words I didn't understand to their responses and this led me to the answer as some subjects they mention encompass a lot of knowledge. It at least shows a willingness to understand the answers you are given.

    Where I am at now I usually get a buzz word like "quaternion" or "SLERP". While the poster has the knowledge of the subject they hardly have the time to explain its every use and implementation. Without them showing me these words though i would spend days trying to find a solution to my poorly worded google search! Lol

    That being said I have seen you progress on this project for a while and you definitely show an aptitude for learning. I have not really messed around with qt or controllers yet, as I continue to live a fantasy that I may one day be a game designer.
    Last edited by Lesshardtofind; 01-20-2013 at 08:54 PM.
    Salem, nvoigt, Elysia and 1 others like this.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    https://www.google.se/search?q=raii
    Also, as for destructor and smart class, go read a little about classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    I thank you Lesshardtofind, when i started doing this type of automation, it was in dos batch files, Basic,console programs and parallel ports!! Windows seemed to make things more difficult once they blocked direct access to ports starting in i believe windows 2000/xp. Decided to but the vellman k8055 board as a usb interface, as it installs as a HID, and doesnt need any "special" software, or drivers. and in console still works for me almost like the parallel ports used to. BUT then i decided to use GUI and touchscreen to control 320 relays. Still have alot to learn of course, and jumping right into it (like i do most things!) i have already learned alot. And if i kep to just one dialog box, or just one script, still works. long way to go though!

    ELYSIA, thank you, read up on RAII, and smart pointers, as well as a little on destructor. so far, not as helpful with my program that i can see so far. STILL tryng to really understand classes, as well as how it would help with sharing a function. Classes still a little confusing!

    ill keep going with it, and hopefully it will work!

    thanks all

  5. #20
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    well started reading classes, went right over my head!!!

    also started reading about function pointers, also kinda lost...

  6. #21
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I think you have a tendency to take a huge project to solve a small problem. That backfires. If you encounter a problem, build the smallest compilable example and solve your problem there, then transfer your solution.

    In this case, your problem is calling a function from one cpp file in another cpp file. No QT, no external dll, nothing. Just two cpp files. Make a new project, add 2 cpp files and solve your problem. Only when it's solved, transfer your solution back to your huge project.

    Somebody already told you above but I will try to rephrase that:

    main.cpp
    Code:
    int main()
    {
    
    }
    
    int function(int x)
    {
    return x*2;
    }
    other.cpp
    Code:
    void other_function(int z)
    {
    return function(z) + 17;
    }
    This will not compile as you already found out. Because you are missing a prototype of "function" in other.cpp. To make sure this prototype is the same across all cpp files, we put those prototypes in extra files called headers.

    function_header.h
    Code:
    int function(int x);
    Now both files need to know that this header exists so you need to #include it. Add

    Code:
    #include "function_header.h"
    to the top of your other.cpp file. Now it should compile.
    Salem likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #22
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    well started reading classes, went right over my head!!!
    If I were you I would just spend more time on it. There is no rule that says you have to get something right away. I think it took me a week to get the right collision algorithm for walking on sloped surfaces.

    I can say from my past that every program I wrote in c++ without classes was poorly written and poorly designed. The moment you understand classes your programs will immediately become more organized and hopefully more re-usable as well. As for function pointers I really have only used them when required by a windowing interface like GLUT.

    I agree with nvgoit on the fact that you seem to be overcomplicating the solution. Sometimes it helps to take a step back from coding and researching and just sketch out a map of all your functions (and classes eventually) and how they interact with each other. In the long run thinking about everything and asking yourself questions like. "Do these really need to interact?". "Does x NEED to know about y?".

    Is going to lead to better design, input to output flow, and less unforeseen bugs. Some things just have to be learned as you go, but I promise that when you "get" classes you will never look back.

  8. #23
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    ok your header and 2 cpp`s worked. but that is because in the CPP`s, the functions are defined.

    unfortunatly i cannot "define" the functions i am using from the dll, because i do not know them!!! just how to call them

    and when i try to do the header, exact to what you showed

    Code:
    intOpenDevice(intCardAddress);
    


    and included the headers in both sources, i get:

    L:\relaydlltest1\main.cpp:16: error: C2365: 'OpenDevice' : redefinition; previous definition was 'function'
    refuring to
    Code:
    t_func4OpenDevice;
    and
    l:\relaydlltest1\Functions.h:5: see declaration of 'OpenDevice'
    going right to
    Code:
    intOpenDevice(intCardAddress);
    i dont understand how i am "overcomplicating the solution", when i have yet to have a solution. i did exactly what both of you said. Make a header that declares the function, and then include it in both sources....

    i did that, and get the same errors i been saying from the start....

    any simplier not "overlycomplicating" idea???





  9. #24
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You have named your functions and variables alike. Give them proper names and your compiler error will vanish.

    t_func4 is a bad name. Tells nothing. How about function_OpenDevice_t or t_func_OpenDevice?

    OpenDevice as variable is a bad name because it's already taken. How about DynamicallyLoadedFunction_OpenDevice?

    And your function to excute that DynamicallyLoadedFunction_OpenDevice could be named CallOpenDeviceFromLibrary. That is the only part that needs to go in the header file.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #25
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    OK started over with a simple opendevice

    functions.h
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    #include <iostream>
    #include <Windows.h>
    
    typedef int(CALLBACK* t_func4)(int );
    t_func4 OpenDevice;
    #endif // FUNCTIONS_H
    main.cpp
    Code:
    #include "dialog.h"
    #include <QApplication>
    #include <windows.h>
    #include <iostream>
    #include <QDebug>
    #include "functions.h"
    int init(void);
    HINSTANCE hDLL;
    int h = init();
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
        return a.exec();
    }
    int init(void)
    {
        hDLL = LoadLibrary((LPCWSTR)L"k8055d");
        if (hDLL != NULL)
        {
             OpenDevice = (t_func4) GetProcAddress(hDLL, "OpenDevice");
            if (!OpenDevice)
            {      // handle the error
                FreeLibrary(hDLL);
                return -2;
                qDebug() << h;
            }
            return 0;    // ok
        }
        return -1;     // error load DLL
    }
    dialog.cpp
    Code:
    #include "dialog.h"
    #include "ui_dialog.h"
    #include "functions.h"
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        OpenDevice(0);
    }
    Dialog::~Dialog()
    {
        delete ui;
    }
    Errors as follows

    dialog.obj:-1: error: LNK2005: "int (__stdcall* OpenDevice)(int)" (?OpenDevice@@3P6GHH@ZA) already defined in main.obj

    debug\dlltest2.exe:-1: error: LNK1169: one or more multiply defined symbols found

    ALOT less errors, then again i am only trying to load/call one function from dll....

    i know it is something simple i must be missing....

    and hard to know what to change/edit as

    1) using the supplied DLL to send to the PIC with custom boot
    2) trying to edit there "demo" and "sample" code
    3) have no idea how to work with multiple sources (like stated before i have always only used 1 source file)

    i learned to program on my own, with help from others here and there when it came to something i never knew.
    so i understand the "i am not doing it for you" which i love. but i do need more then just a "read the book" type answer as the "books" i have found, are no help with this!

    a broke down simple example would be freaken great instead of 4 days of VERY general answers, even after stating i have no idea how to write headers, very vague idea of how to load a DLL (as i learned from 40 different pages, which all had something different) to be honest, i am getting sick of the "try this" with my reply of "nope", then "well you need to fix it" with my reply being "how" and the response "i already told you, with a header" it has become a 4 day never ending circle. Even when i teach in lab, when people have tried, i atleast break it down to simple terms that they (who have NEVER programmed before) can understand. Well with the DLL`s, Headers, Multiple sources, QT Creator, 4 things i have NEVER used untill about a week ago, i need a little more explination of what the hell i am doing....
    Last edited by Crossfire; 01-23-2013 at 04:05 AM.

  11. #26
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    oh and not to forget, following the "MAKE A DLL AND USE IT" lessons

    i made a DLL, and then including both the headers, trying to load it, all i ever got was

    L:\MyLib-build-Desktop-Debug\debug\MyLib.dll:-1: error: LNK1107: invalid or corrupt file: cannot read at 0x2C8

    and that was using the same headers that made the dll, so making a header for a dll so that i can use a function call no matter where i call it in the application is driving me nuts, i have rewrote this program about 15 different times, atleast 15 different ways, and end up with the same result, or worse.

    so honestly, no, nothing here has seemed helpful.
    Last edited by Crossfire; 01-23-2013 at 04:12 AM.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Unfortunately, that is how many things are. There are really never clear answers. There are only bits and pieces. You have to puzzle them together. Yes, I know it is frustrating, but you have to get used it because this is not the last time you'll have to deal with it.
    Anyway, some general pointers and what is going wrong.

    In functions.h, you have:

    t_func4 OpenDevice;

    And this is your problem. You've declared a global variable, which is included in both .cpp files. This mean you have two variables with the same name that are globals. That is an error and that is why you get an error saying it is already defined.
    The fix? The best fix is to avoid global variables altogether. Failing that, you need to define the variable in one and only one .cpp file. Then, in the header, declare it as

    extern t_func4 OpenDevice;

    This essentially tells the compiler that it should not define a variable (because of "extern"), but also at the same time tells the compiler that it exists, but in another source file. So, so long as you have defined it in one source file, it will not complain.
    Now, some general pointers:

    Code:
    int init(void) // Using "void" when no arguments are needed is just an ugly relic from C. You can get rid of it.
    {
        hDLL = LoadLibrary((LPCWSTR)L"k8055d"); // Avoid global variables as much as possible. Don't cast strings.
        if (hDLL != NULL) // This is an anti-pattern. Use if (!hDLL), then terminate.
        {
             OpenDevice = (t_func4) GetProcAddress(hDLL, "OpenDevice");
            if (!OpenDevice) // Same here. Anti-pattern.
            {      // handle the error
                FreeLibrary(hDLL); // Cleanup should be done via RAII
                return -2;
                qDebug() << h; // This will never get executed.
            }
            return 0;    // ok
        }
        return -1;     // error load DLL
    }
    Based on that, an improved version of the code might be:
    Code:
    int init() // Using "void" when no arguments are needed is just an ugly relic from C. You can get rid of it.
    {
    	HINSTANCE hDLL = LoadLibrary(L"k8055d");
    	if (!hDLL)
    		return -1;
    
    	OpenDevice = (t_func4) GetProcAddress(hDLL, "OpenDevice");
    	std::shared_ptr<void*> _(nullptr, [hDLL](void*) { FreeLibrary(hDLL); });
    
    	if (!OpenDevice)
    		return -2;
    
    	return 0;    // ok
    }
    Some of this code requires C++11 (see sig).
    Crossfire likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    extern t_func4 OpenDevice; killed the error, like i said, i knew it was something simple i was missing, and that your for explainging what it does also!

    the void, and the debug, a few to many CTRL-Z while i was "editing"

    will work on fixing the globals, and i dont use them unless i have to. This was all just slap together to figure out my error. Thank you VERY much


    //EDIT//

    ok now confused, worked, then second run, it didnt, undid the EXTERN so that it wasnt there anymore, and now it runs.....WTH?

    //edit 2//
    now it doesnt work with or without extern

    //Edit 3//

    ok fixed edit 1 and 2, now to find out why it runs.....then crashes!
    Last edited by Crossfire; 01-23-2013 at 05:21 AM.

  14. #29
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Crossfire, I can understand that this is quite frustrating. C++ isn't easy. But you need to start with the small (or not so small) basics like header files and classes. A project that dynamically loads a dll in a cross platform gui framework is way out of your league if classes and RAII aren't in your reach yet.

    I'll take a wild guess and say that you have no idea what this line does:
    Code:
    std::shared_ptr<void*> _(nullptr, [hDLL](void*) { FreeLibrary(hDLL); });
    And that's ok. It's freaky stuff. But you cannot base your program on something that you don't understand. That simply won't work.

    For example, if I'm not mistaken, that line tells me that the library will be unloaded at the end of the init function when the shared_pointers destructor (after making sure it was the only reference) calls the lambda expression. So your global variable with the function pointer will propbably be invalid after init.

    I will repeat my advice: divide and conquer. Your program fails and you don't know why. Make your problem set smaller. You tackled multiple cpp files and headers. Now forget about multiple files and your Gui framework. Can you write the smallest possible program dynamically loading your dll and calling a function? If you can, combine the solutions.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  15. #30
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    yeah and i agree, i already can load and have loaded the dlls, just never knew how to make them "global" for the entire application.

    and i agree, i started small, have an entire programs in C, C++, and Windows forms to do this. Just trying to "port" it over to gui now using QT!

    and once i had that one running in only one form, owith nce source, i decided to make it span accross dialogs. starting with just opening the device in a new dialog.

    being able to call the function from the new dialog is where i was stuck. But i do that all of you for helping so far!

    sorry for my fustration, been beating my skull against the keyboard! lol

    and your right, i have yet to study/use the c++11 yet, so didnt have a clue to what that did!

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  2. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  3. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21