Create and link a .dylib in Xcode in C - c

I'm stuck with a problem.
Well, I want to create my own dylib file, and inside of it write some methods, etc. You know, just a normal library.
Ok, so I opened Xcode, New project, and I selected Dynamic library. I've created a *main_lib.c* file, and wrote this method inside of it:
void printMessage(char *str){
printf("%s", str);
}
Obviously, I linked this file with the .dylib file and builded the project, no errors. After this, I created another project, selected Command Line Application and created a main.c file.
I imported the builded .dylib file in this project.
I asked one friend of mine, he said that at this point I should create an header file and write inside of it the function prototype of the dylib. So I created a main_lib.h file and declared this prototype:
void printMessage(char *str);
At this point, I write in the main.c file this code:
#include <stdio.h>
#include "main_lib.h"
int main(void){
printMessage("just a try");
return 0;
}
But when I build this it says:
ld: symbol(s) not found for architecture x86_64
It means that the compiler cannot find printMessage, right? But why? What am I doing wrong? Please help :) Thanks in advance guys!

To include it just add it to Target Dependencies and Link With Libraries in "Build Phases" and thats it, it should work.

Related

Include Files in Segger Embedded Studio

I am just starting out with Segger Embedded Studio. Right now trying to call a function of a included header file. The file seems to be included since it shows up in the dependencies. For now I simply included the header and C-File in the project directory.
The included Header- and C-Files are simply:
//##### Header-File ######
#ifndef TEST_H_
#define TEST_H_
void printText(void);
#endif //TEST_H_
and:
//###### C-File #########
#include <test.h>
#include <stdio.h>
#include <stdlib.h>
void printText(void)
{
printf("Hello");
}
But when I try to call the printText Function in my main I get the error:
"Undefined Symbol: printText".
Why is the function not recognized?
"Undefined symbol" is a linker error. You are not linking the object code containing the definition of printText().
It is not an issue with the header file; including a header file does not cause the associated code to be linked - that is just the declaration so the compiler knows what the interface looks like. It is the linker than combines the separately compiled object code to form a program. You have not told the linker to use the object code containing printText(), and you have not told your IDE project to compile it to generate that object code.
The project tree clearly shows that only main.c is included in your project; you need to add the C file containing printText() too.
The concept of separate compilation and linking is what you need to grasp here.
Thank you Clifford for your answer. You are right I had some miss-conceptions about which files will be linked while building the project. In the special case of segger embedded studio there are, as I know by now, two ways to reference extern files.
Adding the files to the sources files folder is straight forward, but must be done separately for each project you want to use the respective files.
For frequently used files it is beneficial to create a dedicated library solution. Such a library can then be imported to any solution by choosing "add existing project". This will add all files of the library to your current solution (and show them in the project-tree). Now click the tab project -> dependencies. Your library should show up here. By acitivating the check box the linker will compile the referenced project upon builing your solution, allowing for the usage of your library functions.
Adding and linking library-project to a current solution

Netbeans - How to add external C libraries? IUP

I'm learning C, and I want to use my first external library.
Firstly I'll show the simplistic code I'm trying to compile:
#include <stdlib.h>
#include <iup.h>
int main(int argc, char **argv)
{
IupOpen(&argc, &argv);
IupMessage("Hello World 1", "Hello world from IUP.");
IupClose();
return EXIT_SUCCESS;
}
The external library being IUP.
I have downloaded the following to my documents folder:
Then I configured Netbeans build options by first adding the header file directory:
Then finally I added the .dll file:
https://prnt.sc/slf1z8
(I added a link because I thought the post was getting too long)
However, while the code no longer shows a red underline under the functions I'm trying to use, the compiler shows "cannot find -liup"
I don't know what could be going wrong. I was following this link tutorial:
http://webserver2.tecgraf.puc-rio.br/iup/en/ide_guide/netbeans.html
Thank you!
When you link, even using a DLL, you need a ".lib" or ".a" file depending on the compiler. In this case this library is called an import library, and contains just "pointers" to the functions in the DLL.
The IUP packages include those import libraries you need. Probably you downloaded package iup-3.29_Win64_dllw6_lib.zip which include the ".a" files you will need.

Open new C project with eclipse for new developer

I want to start learning C and download Eclipse for C\C++ and want to start new project.
I found this tutorial and after create new project, choose C project with MinGcc and hit finish this is the errors i got:
Symbol 'EXIT_SUCCESS' could not be resolved
What i am doing wrong ?
EXIT_SUCCESS is defined in stdlib.h:
#include <stdlib.h>
int main(void)
{
return EXIT_SUCCESS;
}
Based on your comments, your problem appears to be with Eclipse. You might want to try off the main menu:
Project->C/C++ Index->
Freshen All Files
Update with Modified Files
Re-resolve Unresolved Includes
Search for Unresolved Includes
In the Project Exlorer tree, it lists where Eclipse CDT looks for include files. You should see a directory in there that holds stdlib.h. On my machine it is in c:\MinGW\include.

Linking to a DLL I created

I am trying to create my own DLL and then make another project load it statically.
My DLL file contains both a header file (called HelloFunc.h):
#include <stdio.h>
extern "C"
{
_declspec(dllexport) void HelloFromDll();
}
And a c file (called HelloFunc.cpp):
#include <stdio.h>
extern "C"
{
_declspec(dllexport) void HelloFromDll()
{
printf("Hello DLL. \n");
}
}
After building the project an Object File Library (.lib) was created.
Then, on my other project I tried to link to it statically.
In linker -> Input -> Additional Dependencies I added my library (I put it in my new project's directory) and then in linker -> Input -> Command Line I saw that it actually linked to it.
However, when I tried to call HelloFromDll() function in my new code, an error says that it is not identified. Note that I also included "HelloFunc.h" but an error says that the source file couldn't be opened.
I'm a little lost and don't know what I've done wrong. Any help will be appreciated :)
You must specify __declspec(dllimport) instead of __declspec(dllexport) when importing a library.
What error message did you received exactly?
[Edited]
When you compile a DLL, you specify __declspec(dllexport). When you compile an application that imports the DLL, you specify __declspec(dllimport).
The problem is that the compiler cannot find HelloFunc.h: Simply copy HelloFunc.h into your new project's directory.
Check that you client & library were compiled in the same mode (debug or release).
This is common bottleneck.

How to link two source files properly? undefined reference error

Ok so I am doing a final project for one of my classes and trying to do a bit extra and create multiple files to work with. I am coding inside of CodeBlocks. So far I have a main.c, levels.c, and levels.h for my files. Inside of the levels.c levelOne function, I put the printf statement as a test to make sure I could have the two files work with each other before I went forward in my coding. I got a "undefined reference to 'levelOne' when I compiled and ran the program.
Inside my main.c file:
#include <stdio.h>
#include <stdlib.h>
#include "levels.h"
int main()
{
levelOne();
return 0;
}
Inside my levels.h file:
#ifndef LEVELS_H_INCLUDED
#define LEVELS_H_INCLUDED
void levelSelect(char c);
void levelOne();
void levelTwo();
void levelThree();
void levelCustom(int difficulty);
#endif // LEVELS_H_INCLUDED
Inside my levels.c file:
void levelOne()
{
//level scope of 1 to 10
srand(time(NULL));
int randomNum = (rand() % 9)+1);
printf("the random number is: %i\n", randomNum);
}
levels.c is not getting passed into the compiler, are you sure you have included levels.c in the whole project? If not it will not link. You need a project if you want to compile multiple files. In CodeBlocks, the sources and the settings for the build process are stored in a project file <name>.cbp
Here is the User Manual
gcc levels.c main.c should link successfully. gcc main.c will only compile one file and try and link to create final executable and levelOne() will not be found. since it is in file levels.c
You need to include levels.h in levels.c as well or if a function (physically) above levelOne calls it, it is undefined.
Then compile it with gcc -Wall *.c -o myapp to compile and link all of the c files in that directory into myapp (or you can name them individually) with (almost) all warnings enabled. This is provided you have it in its own directory.
Once you get into larger projects with more code, you can compile individual .c files into .o object files with gcc -Wall -c somecode.c and then link all the objects with gcc *.o -o myapp. If it gets really large, you'll want a build system to help with rebuilding objects only when its code (or dependent code) changes (such as Makefiles, waf, and dare I say autotools).
I had this exact same problem, the solution is easy. Right click on levels.c and select properties. A properties window should come up select the "Build" tab tick compile file, link file, and in the box check debug and release. This should fix your problem.
Don't make the mistake of doing this with a header file because it will give you a "...h.gch: file not recognized: File format not recognized.." error.

Resources