I compiled a simple program:
#include <stdio.h>
#include <floatfann.h>
#include <fann.h>
int main(int argc, char* argv[]) {
struct fann *ann = fann_create_standard(3, 28 * 28, 49, 10);
fann_train_on_file(ann, "train.fann", 200, 10, 0.0001);
fann_save(ann, "trained-1-0.7-3-784-49-10.net");
fann_destroy(ann);
return 0;
}
I get the following error at RUNTIME:
symbol lookup error: /usr/lib/libfann.so.2: undefined symbol: log
The compilation command is:
gcc -lm -lfann train_acc.c -o train_ann
I already read this discussion and this other. I already tried to install, uninstall, remove, update every possible combination of libraries. I tried to download and rebuild the sources from FANN website, I tried to switch from x64 to x86. Still I get the same error.
I am running Ubuntu 13.04 64 bit.
Did you try changing the linking order?
gcc train_acc.c -o train_ann -lm -lfann
Related
I am running Xubuntu on a vm (because it was easier to do that compared to getting gcc on windows).
I have tried all of the different arguments for compiling the program (eg. running gcc filename.c and gcc filename.c -o filename), but none of them have changed the outcome.
My code is:
#include <stdio.h>
int main(void) {
printf("a");
return 0;
}
I've tried using main() and main(arg...[or whatever it is I can't remember]) and I get the same error.
I wrote some C code on VSCODE like that.
The code is divided into three files: the header, the function, and main in same project folder.
But when I started compile, files are can't compile and error. like terminal text.
Maybe I think this error is linking error..
How to solve this problem..?
[source code]
mysqrt.c
#include "mysqrt.h"
#include <stdio.h>
#include <math.h>
double mysqrt(double a, double b){
double result = sqrt(pow(a,2)+pow(b,2));
return result;
}
mysqrt.h
#include <stdio.h>
double mysqrt(double a, double b);
mysqrtTest.c
#include <stdio.h>
#include <math.h>
#include "mysqrt.h"
void main(void){
double sum = mysqrt(3,4);
printf("%.2f\n",sum);
}
[terminal text]
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: warning:
return type of 'main' is not 'int' [-Wmain-return-type]
void main(void){
^
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: note: change
return type to 'int'
void main(void){
^~~~
int
1 warning generated.
Undefined symbols for architecture x86_64:
"_mysqrt", referenced from:
_main in mysqrtTest-45c3c1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[OS]: macOS Mojave 10.14.6
to resolve your linking issue you need to compile all your c file, as dependencies are not automatically resolved ( header files could be name separately from code file so .h files and .c files are independant ).
# assuming that gcc is your compiler
gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c -lm
Although I would recommend you to learn about separate compilation, and using a build system, like make
Example of Makefile
mysqrtTest: mysqrtTest.o mysqrt.o
${CC} -o $# $^ -lm
then use make to build your binary
I have a c script
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(void)
{
const gsl_rng_type * T;
gsl_rng * r;
int i;
double a,b;
double num;
a=10;
b=7.2;
gsl_rng_env_setup();
T=gsl_rng_default;
r=gsl_rng_alloc (T);
for(i=0; i<10; i++)
{
num = gsl_ran_gamma(r,a,b);
printf("%.8f \n",num);
}
gsl_rng_free(r);
return 0;
}
Which I have successfully compiled on a linux machine. I want to use the gsl library for other applications on my mac. So I first installed gsl using homebrew which seemed to be successful. To make sure everything was working right I tried to compile and run this script as follows
[ACC-259-imac:GDSC Gene Expression Modeling jmannhei$ gcc -Wall gamma.c -o gamma.out -lm -lgsl -lgslcblas
which resulted in the following output
gamma.c:5:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
Which is exactly how I compiled it on Linux so I am not sure what is awry as I have compile c scripts in the past using this format from terminal on a mac before. My guess is it is not linking properly but I am not sure what I need to do to fix it. Thanks
Find your header file directory with brew list gsl or locate gsl/gsl_rng.h then tell your compiler where the headers are. I would need the following, for example,
gcc -Wall -I/usr/local/Cellar/gsl/1.16/include/ gamma.c -o gamma.out -lm -lgsl -lgslcblas
We were given course work to create the game Mine Sweeper. We are still early in the semester so this homework shouldn't be too difficult.
We were given header and source files that are to be used for the visual part of the program.
Main problem is that I can't compile these files on my Mac. Here is what I get:
$ gcc mineSweeper.c -I.
Undefined symbols for architecture x86_64:
"_colorPrint", referenced from:
_main in mineSweeper-4b9486.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Also tried this:
$ gcc mineSweeper.c -I. -arch i386
Undefined symbols for architecture i386:
"_colorPrint", referenced from:
_main in mineSweeper-0938b1.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gcc version:
gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
OSX version:
Software OS X 10.9.5 (13F34)
And finally the code we were provided with:
//colorPrint.h
//defines possible colors for the foreground color.
typedef enum
{
FG_Def = 0,
FG_Black = 30,
FG_Red,
FG_Green,
FG_Yellow,
FG_Blue,
FG_Magenta,
FG_Cyan,
FG_White
}fgColor;
//defines possible colors for the background color.
//BG_Def prints with the default background color of the terminal.
typedef enum
{
BG_Def = 0,
BG_Black = 40,
BG_Red,
BG_Green,
BG_Yellow,
BG_Blue,
BG_Magenta,
BG_Cyan,
BG_White
}bgColor;
//defines possible additional attributes for the color printing.
//normally, you would use ATT_Def.
typedef enum
{
ATT_Def = 0,
ATT_Bright = 1,
ATT_Underline = 4,
ATT_Reverse = 7,
ATT_Hidden = 8,
ATT_Scratch = 9
}attribute;
//clears the screen completely.
void clearScreen();
//prints a format string with its arguments (like printf!),
//in the specified foreground color, background color, and attribute.
void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...);
//colorPrint.c
#include <stdio.h>
#include <colorPrint.h>
#include <stdarg.h>
void clearScreen()
{
printf("\e[1;1H\e[2J");
}
void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...)
{
va_list args;
if(bg != BG_Def)
printf("\e[%d;%d;%dm",att,fg,bg);
else
printf("\e[%d;%dm",att,fg);
va_start (args, format);
vprintf(format, args);
va_end (args);
printf("\e[0m");
}
There is another header and code for receiving the char from user but I'm assuming linking it is irrelevant.
Any sort of help is welcome.. thanks in advance :)
PS. I also have a PC if it helps to switch to windows.
PPS. I'm keeping VirtualBox as a last resort.
You're trying to compile and link mineSweeper.c into a final executable on its own, but that file is not a complete program, it depends on a function defined in another file.
You either need to compile and link all the files in one step:
gcc mineSweep.c colourPrint.c
or compile each file separately and then link the objects:
gcc -c mineSweeper.c
gcc -c colorPrint.c
gcc mineSweeper.o colorPrint.o
I'm surprised your course didn't explain how to compile programs consisting of more than one file.
A simple makefile will ease the process:
mineSweeper: mineSweeper.o colorPrint.o
$(CC) $^ $(LDLIBS) -o $#
I am trying to build an openssl simple program. Here is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16];
unsigned char out_[16];
strcpy(userKey_,"0123456789123456");
strcpy(in_,"0123456789123456");
fprintf(stdout,"Original message: %s", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s", in_);
return 0;
}
I try to compile it using this command:
gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c
and I get this:
/tmp/ccT1XMid.o: In function `main':
tema1.c:(.text+0x8d): undefined reference to `AES_set_encrypt_key'
tema1.c:(.text+0xa7): undefined reference to `AES_encrypt'
tema1.c:(.text+0xbf): undefined reference to `AES_set_decrypt_key'
tema1.c:(.text+0xd9): undefined reference to `AES_decrypt'
collect2: ld returned 1 exit status
I am under Ubuntu 10.04. How can I get this to work ?
You may be trying to statically link, but the -L option and -lcrypto are looking for a file to link with dynamically. To statically link to a specific library, just specify your .a file on the compiler command line after all your source files.
E.g.,
gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
For those of you who have this same problem but are using Windows, Mingw and this OpenSSL for Windows (at this time: Win32 OpenSSL v1.0.2a). You need to link to libeay32.a that is located in C:\OpenSSL-Win32\lib\MinGW\ (after installing OpenSSL).
In my case I am using CMake and the powerful CLion IDE, so I had to rename the library to libeay32.dll.a because CMake wasn't locating the library. This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
project(openssl_1_0_2a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(C:\\OpenSSL-Win32\\include)
set(SOURCE_FILES main.cpp)
link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
add_executable(openssl_1_0_2a ${SOURCE_FILES})
target_link_libraries(openssl_1_0_2a eay32)
I made the test with this example (which is borrowed from this answer):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16] = {0};
unsigned char out_[16] = {0};
strcpy((char *) userKey_,"0123456789123456");
strcpy((char *) in_,"0123456789123456");
fprintf(stdout,"Original message: %s\n", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s XXX \n", in_);
return 0;
}
I think the order of the parameter should be reset like follows:
gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c -L . -lssl -lcrypto